Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
Co-authored-by: Thomas Countz <[email protected]>
  • Loading branch information
HeyNonster and Thomascountz committed Nov 6, 2023
1 parent 2fcbae4 commit 05e87f3
Show file tree
Hide file tree
Showing 13 changed files with 381 additions and 168 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ jobs:
gemfile:
- rails6.1
- rails7.0
legacy_connection_handling:
- 'true'
- 'false'
include:
- {ruby-version: '2.7', gemfile: rails6.0}
- {ruby-version: '3.0', gemfile: rails6.0}
- {ruby-version: '2.7', gemfile: rails6.0, legacy_connection_handling: 'false'}
- {ruby-version: '3.0', gemfile: rails6.0, legacy_connection_handling: 'false'}
env:
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
LEGACY_CONNECTION_HANDLING: ${{ matrix.legacy_connection_handling }}
steps:
- uses: zendesk/checkout@v2
- name: Set up Ruby
Expand Down
4 changes: 4 additions & 0 deletions lib/property_sets.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ def self.ensure_property_set_class(association, owner_class_name)
end

def self.parent_for_property_class(namespace, owner_class_name)
if namespace.const_get(owner_class_name).property_sets_connection_class
return namespace.const_get(owner_class_name).property_sets_connection_class
end

namespace.const_get(owner_class_name).connection_class_for_self
rescue NameError
::ActiveRecord::Base
Expand Down
2 changes: 2 additions & 0 deletions lib/property_sets/active_record_extension.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
module PropertySets
module ActiveRecordExtension
module ClassMethods
attr_accessor :property_sets_connection_class

def property_set(association, options = {}, &block)
unless include?(PropertySets::ActiveRecordExtension::InstanceMethods)
self.send(:prepend, PropertySets::ActiveRecordExtension::InstanceMethods)
Expand Down
2 changes: 1 addition & 1 deletion property_sets.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new "property_sets", PropertySets::VERSION do |s|
s.add_development_dependency("rake")
s.add_development_dependency('actionpack')
s.add_development_dependency('rspec')
s.add_development_dependency('byebug')
s.add_development_dependency('pry-byebug')

s.files = `git ls-files lib`.split("\n")
s.license = "MIT"
Expand Down
2 changes: 0 additions & 2 deletions spec/inheritance_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
require 'active_record'
require 'property_sets'

ENV["RAILS_ENV"] = "test"

yaml_config = "spec/support/database.yml"
ActiveRecord::Base.configurations = begin
YAML.safe_load(IO.read(yaml_config), aliases: true)
Expand Down
46 changes: 33 additions & 13 deletions spec/property_sets_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
PropertySets::PropertySetModel::COLUMN_TYPE_LIMITS.merge('varchar' => 65535)
$-w = old

# RSpec.shared_examples "some example" do |parameter|
# it "construct the container class" do
# end
# end

describe PropertySets do
let(:account) { Parent::Account.create(:name => "Name") }
let(:relation) { Parent::Account.reflections["settings"] }
Expand Down Expand Up @@ -33,14 +38,14 @@

it "allow the owner class to be customized" do
(Flux = Class.new(ActiveRecord::Base)).property_set(:blot, {
:owner_class_name => 'Foobar'
}) { property :test }
:owner_class_name => 'Foobar'
}) { property :test }

expect(defined?(FoobarBlot)).to be_truthy
end

it "pass-through any options from the second parameter" do
class AnotherThing < ActiveRecord::Base
class AnotherThing < MainDatabase
self.table_name = "things" # cheat and reuse things table
end

Expand All @@ -50,6 +55,12 @@ class AnotherThing < ActiveRecord::Base
expect(AnotherThing.new.settings.extensions).to include(::Parent::Account::Woot)
end

end

RSpec.shared_examples "different account models" do |account_klass|
let(:account) { account_klass.create(:name => "Name") }
let(:relation) { account_klass.reflections["settings"] }

it "support protecting attributes" do
expect(account.settings.protected?(:pro)).to be true
expect(account.settings.protected?(:foo)).to be false
Expand Down Expand Up @@ -111,7 +122,8 @@ class AnotherThing < ActiveRecord::Base
end

it "reject settings with an invalid name" do
s = Parent::AccountSetting.new(:account => account)
settings_klass = Object.const_get("#{account_klass.to_s}Setting")
s = settings_klass.new(account.model_name.element.to_sym => account)

valids = %w(hello hel_lo hell0) + [:hello]
invalids = %w(_hello)
Expand Down Expand Up @@ -157,7 +169,7 @@ class AnotherThing < ActiveRecord::Base
it "reference the owner instance when constructing a new record" do
record = account.settings.lookup(:baz)
expect(record).to be_new_record
expect(record.account.id).to eq(account.id)
expect(record.send(account.model_name.element.to_sym).id).to eq(account.id)
end

it "reference the owner instance when constructing a new record ...on a new record" do
Expand Down Expand Up @@ -190,8 +202,8 @@ class AnotherThing < ActiveRecord::Base

it "return all property pairs if no arguments are provided" do
expect(account.settings.get.keys.sort).to eq(
%w(bar baz bool_false bool_nil bool_nil2 bool_true foo hep pro).sort
)
%w(bar baz bool_false bool_nil bool_nil2 bool_true foo hep pro).sort
)
end

it "ignore non-existent keys" do
Expand Down Expand Up @@ -259,9 +271,9 @@ class AnotherThing < ActiveRecord::Base
expect(account.texts.bar).to eq("0")

account.update_attributes!(:texts_attributes => [
{ :id => account.texts.lookup(:foo).id, :name => "foo", :value => "0" },
{ :id => account.texts.lookup(:bar).id, :name => "bar", :value => "1" }
])
{ :id => account.texts.lookup(:foo).id, :name => "foo", :value => "0" },
{ :id => account.texts.lookup(:bar).id, :name => "bar", :value => "1" }
])

expect(account.texts.foo).to eq("0")
expect(account.texts.bar).to eq("1")
Expand Down Expand Up @@ -368,23 +380,23 @@ class AnotherThing < ActiveRecord::Base
it "creates changed attributes" do
account.update_attribute(:old, "it works!")
expect(account.previous_changes["old"].last).to eq("it works!")
expect(Parent::Account.find(account.id).old).to eq("it works!")
expect(account_klass.find(account.id).old).to eq("it works!")
end

it "updates changed attributes for existing property_set data" do
account.settings.hep = "saved previously"
account.save
account.update_attribute(:old, "it works!")
expect(account.previous_changes["old"].last).to eq("it works!")
expect(Parent::Account.find(account.id).old).to eq("it works!")
expect(account_klass.find(account.id).old).to eq("it works!")
end

it "updates changed attributes for existing property_set data after set through forwarded method" do
account.old = "saved previously"
account.save
account.update_attribute(:old, "it works!")
expect(account.previous_changes["old"].last).to eq("it works!")
expect(Parent::Account.find(account.id).old).to eq("it works!")
expect(account_klass.find(account.id).old).to eq("it works!")
end
end

Expand Down Expand Up @@ -510,3 +522,11 @@ class AnotherThing < ActiveRecord::Base
end
end
end

describe Parent::Account do
it_behaves_like "different account models", Parent::Account
end

describe Parent::AccntOtherDb do
it_behaves_like "different account models", Parent::AccntOtherDb
end
16 changes: 14 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
require 'active_record'
require 'active_record/fixtures'

ENV["RAILS_ENV"] = "test"

LEGACY_CONNECTION_HANDLING = (ENV["LEGACY_CONNECTION_HANDLING"] == "true")

case "#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"
when '7.0'
ActiveRecord.legacy_connection_handling = LEGACY_CONNECTION_HANDLING
when '6.1'
ActiveRecord::Base.legacy_connection_handling = LEGACY_CONNECTION_HANDLING
end

require 'property_sets'
require 'property_sets/delegator'

require 'support/database_setup'
require 'support/models'
require 'support/database'
require 'support/account'
require 'support/thing'

I18n.enforce_available_locales = false

Expand Down
61 changes: 0 additions & 61 deletions spec/support/account.rb

This file was deleted.

Loading

0 comments on commit 05e87f3

Please sign in to comment.