superstore 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 520a74498d7f283f4f017694aec4df86a722599a
4
- data.tar.gz: 3609a0c05c75b783d80a2f0b18aa97befa26278d
3
+ metadata.gz: bcfb4894f1b5070e7d844c0e4c24e64d8616b6b2
4
+ data.tar.gz: 09a982cfedfcb5fc0998296fed5c4143530abec2
5
5
  SHA512:
6
- metadata.gz: f9978f4c88be6d13cdd5f89c054657a255a2f7584fd93bd13c3fc58c0bc69f12d80933f0af9d2059cd8bf1a75d4f17e1b4df6f1493f827061a5c26fc79b95267
7
- data.tar.gz: 0d9bc3f395451d5d6d5aac5c4af148775ca66e09b9c933fcc75eb186414a899e305ff74a804a4dd1ae187667bbd8a2324ed6354d6d9c916b061a5d20eb7b9363
6
+ metadata.gz: 4385589c6b5e8904dfb4a38d0de81017d5d3c34cffb21e0ef36a8248616e76d1fe26a6ede7ef95b4b9b04a25301c1dd1ac053415c0da6a4a7adc22d251624fed
7
+ data.tar.gz: 0a3b380939669676eb9a9a6bc5e5440b806379efc5363f36c86857743547d8c24884f5db8c68bbc0834f734e938b326c380cb701fc0c4ed3e76e9132df5da7a1
@@ -14,7 +14,7 @@ module Superstore::Associations::Builder
14
14
  define_reader
15
15
 
16
16
  reflection = Superstore::Associations::Reflection.new(macro, name, model, options)
17
- model.association_reflections = model.association_reflections.merge(name => reflection)
17
+ ActiveRecord::Reflection.add_reflection model, name, reflection
18
18
  end
19
19
 
20
20
  def mixin
@@ -16,7 +16,7 @@ module Superstore
16
16
  relation.instance_variable_set :@records, records
17
17
  relation.instance_variable_set :@loaded, true
18
18
 
19
- self.target = load_collection
19
+ self.target = relation
20
20
  end
21
21
 
22
22
  private
@@ -2,11 +2,6 @@ module Superstore
2
2
  module Associations
3
3
  extend ActiveSupport::Concern
4
4
 
5
- included do
6
- class_attribute :association_reflections
7
- self.association_reflections = {}
8
- end
9
-
10
5
  module ClassMethods
11
6
  # === Options
12
7
  # [:class_name]
@@ -19,54 +14,29 @@ module Superstore
19
14
  # class Truck < Superstore::Base
20
15
  # end
21
16
  def belongs_to(name, options = {})
22
- Superstore::Associations::Builder::BelongsTo.build(self, name, options)
17
+ if options.delete(:superstore)
18
+ Superstore::Associations::Builder::BelongsTo.build(self, name, options)
19
+ else
20
+ super
21
+ end
23
22
  end
24
23
 
25
24
  def has_many(name, options = {})
26
- Superstore::Associations::Builder::HasMany.build(self, name, options)
25
+ if options.delete(:superstore)
26
+ Superstore::Associations::Builder::HasMany.build(self, name, options)
27
+ else
28
+ super
29
+ end
27
30
  end
28
31
 
29
32
  def has_one(name, options = {})
30
- Superstore::Associations::Builder::HasOne.build(self, name, options)
31
- end
32
-
33
- def generated_association_methods
34
- @generated_association_methods ||= begin
35
- mod = const_set(:GeneratedAssociationMethods, Module.new)
36
- include mod
37
- mod
33
+ if options.delete(:superstore)
34
+ Superstore::Associations::Builder::HasOne.build(self, name, options)
35
+ else
36
+ super
38
37
  end
39
38
  end
40
39
  end
41
40
 
42
- # Returns the belongs_to instance for the given name, instantiating it if it doesn't already exist
43
- def association(name)
44
- instance = association_instance_get(name)
45
-
46
- if instance.nil?
47
- reflection = association_reflections[name]
48
- instance = reflection.association_class.new(self, reflection)
49
- association_instance_set(name, instance)
50
- end
51
-
52
- instance
53
- end
54
-
55
- private
56
- def clear_associations_cache
57
- associations_cache.clear if persisted?
58
- end
59
-
60
- def associations_cache
61
- @associations_cache ||= {}
62
- end
63
-
64
- def association_instance_get(name)
65
- associations_cache[name.to_sym]
66
- end
67
-
68
- def association_instance_set(name, association)
69
- associations_cache[name.to_sym] = association
70
- end
71
41
  end
72
42
  end
@@ -4,8 +4,9 @@ module Superstore
4
4
  extend ActiveSupport::Concern
5
5
 
6
6
  module ClassMethods
7
+ PRIMARY_KEY = 'id'
7
8
  def primary_key
8
- 'id'
9
+ PRIMARY_KEY
9
10
  end
10
11
  end
11
12
 
@@ -33,6 +33,10 @@ module Superstore
33
33
  def attribute_methods_generated?
34
34
  @attribute_methods_generated ||= false
35
35
  end
36
+
37
+ def dangerous_attribute_method?(name)
38
+ false
39
+ end
36
40
  end
37
41
 
38
42
  def write_attribute(name, value)
@@ -40,7 +44,19 @@ module Superstore
40
44
  end
41
45
 
42
46
  def read_attribute(name)
43
- @attributes[name.to_s]
47
+ name = name.to_s unless name.is_a?(String)
48
+
49
+ if name == self.class.primary_key
50
+ send(name)
51
+ else
52
+ @attributes[name]
53
+ end
54
+ end
55
+ alias_method :_read_attribute, :read_attribute
56
+
57
+ def attribute_present?(attribute)
58
+ value = _read_attribute(attribute)
59
+ !value.nil? && !(value.respond_to?(:empty?) && value.empty?)
44
60
  end
45
61
 
46
62
  def attribute_exists?(name)
@@ -11,6 +11,15 @@ module Superstore
11
11
  include ActiveModel::Serializers::JSON
12
12
  include GlobalID::Identification
13
13
 
14
+ extend ActiveRecord::Delegation::DelegateCache
15
+ extend ActiveRecord::ConnectionHandling
16
+ include ActiveRecord::ModelSchema
17
+ include ActiveRecord::Inheritance
18
+ include ActiveRecord::Attributes
19
+ include ActiveRecord::Associations
20
+ include ActiveRecord::AutosaveAssociation
21
+ include ActiveRecord::Reflection
22
+
14
23
  include Model
15
24
  include Core
16
25
  include Connection
@@ -15,6 +15,10 @@ module Superstore
15
15
  raise "Unknown adapter #{config[:adapter]}"
16
16
  end
17
17
  end
18
+
19
+ def connection
20
+ adapter.connection
21
+ end
18
22
  end
19
23
  end
20
24
  end
@@ -2,10 +2,43 @@ module Superstore
2
2
  module Core
3
3
  extend ActiveSupport::Concern
4
4
 
5
+ module ClassMethods
6
+ def inspect
7
+ if self == Base
8
+ super
9
+ else
10
+ attr_list = attribute_definitions.keys * ', '
11
+ "#{super}(#{attr_list.truncate(140 * 1.7337)})"
12
+ end
13
+ end
14
+
15
+ def initialize_generated_modules # :nodoc:
16
+ generated_association_methods
17
+ end
18
+
19
+ def generated_association_methods
20
+ @generated_association_methods ||= begin
21
+ mod = const_set(:GeneratedAssociationMethods, Module.new)
22
+ include mod
23
+ mod
24
+ end
25
+ end
26
+
27
+ def arel_table # :nodoc:
28
+ @arel_table ||= Arel::Table.new(table_name, self)
29
+ end
30
+
31
+ def subclass_from_attributes?(attrs)
32
+ false
33
+ end
34
+ end
35
+
5
36
  def initialize(attributes=nil)
6
- @new_record = true
7
- @destroyed = false
8
- @attributes = {}
37
+ @new_record = true
38
+ @destroyed = false
39
+ @association_cache = {}
40
+
41
+ @attributes = {}
9
42
  self.attributes = attributes || {}
10
43
 
11
44
  yield self if block_given?
@@ -19,6 +52,7 @@ module Superstore
19
52
  @id = nil
20
53
  @new_record = true
21
54
  @destroyed = false
55
+ @association_cache = {}
22
56
  super
23
57
  end
24
58
 
@@ -30,17 +64,6 @@ module Superstore
30
64
  id.hash
31
65
  end
32
66
 
33
- module ClassMethods
34
- def inspect
35
- if self == Base
36
- super
37
- else
38
- attr_list = attribute_definitions.keys * ', '
39
- "#{super}(#{attr_list.truncate(140 * 1.7337)})"
40
- end
41
- end
42
- end
43
-
44
67
  def ==(comparison_object)
45
68
  comparison_object.equal?(self) ||
46
69
  (comparison_object.instance_of?(self.class) &&
@@ -8,14 +8,6 @@ module Superstore
8
8
  end
9
9
 
10
10
  module ClassMethods
11
- def table_name=(table_name)
12
- @table_name = table_name
13
- end
14
-
15
- def table_name
16
- @table_name ||= base_class.model_name.plural
17
- end
18
-
19
11
  def base_class
20
12
  class_of_active_record_descendant(self)
21
13
  end
@@ -43,6 +43,7 @@ module Superstore
43
43
  object.instance_variable_set("@new_record", false)
44
44
  object.instance_variable_set("@destroyed", false)
45
45
  object.instance_variable_set("@attributes", typecast_persisted_attributes(attributes))
46
+ object.instance_variable_set("@association_cache", {})
46
47
  end
47
48
  end
48
49
 
@@ -127,7 +128,7 @@ module Superstore
127
128
  end
128
129
 
129
130
  def reload
130
- clear_associations_cache
131
+ clear_association_cache
131
132
  @attributes = self.class.find(id).instance_variable_get('@attributes')
132
133
  self
133
134
  end
@@ -8,6 +8,9 @@ module Superstore
8
8
  delegate :find_each, :find_in_batches, to: :scope
9
9
  delegate :select, :where, :where_ids, to: :scope
10
10
  end
11
+
12
+ class_attribute :default_scopes, instance_writer: false, instance_predicate: false
13
+ self.default_scopes = []
11
14
  end
12
15
 
13
16
  module ClassMethods
data/superstore.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = 'superstore'
5
- s.version = '2.0.1'
5
+ s.version = '2.1.0'
6
6
  s.description = 'ActiveModel for many attributes'
7
7
  s.summary = 'JSONB ActiveModel'
8
8
  s.authors = ["Michael Koziarski", "Infogroup"]
data/test/test_helper.rb CHANGED
@@ -21,7 +21,7 @@ module Superstore
21
21
  class TestCase < ActiveSupport::TestCase
22
22
  def temp_object(&block)
23
23
  Class.new(Superstore::Base) do
24
- self.table_name = 'Issues'
24
+ self.table_name = 'issues'
25
25
  string :force_save
26
26
  before_save { self.force_save = 'junk' }
27
27
 
@@ -1,95 +1,30 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Associations::BelongsTest < Superstore::TestCase
4
- class TestObject < Issue
5
- string :issue_id
6
- belongs_to :issue
7
-
8
- string :widget_id
9
- belongs_to :widget, class_name: 'Issue'
10
-
11
- string :other_id
12
- belongs_to :other_issue, class_name: 'Issue', foreign_key: :other_id
13
-
4
+ class TestObject < Superstore::Base
5
+ self.table_name = 'issues'
14
6
  string :user_id
15
7
  belongs_to :user, primary_key: :special_id
16
-
17
- string :title_issue_id
18
- belongs_to :title_issue, class_name: 'Issue', primary_key: :title
19
-
20
- string :target_id
21
- string :target_type
22
- belongs_to :target, polymorphic: true
23
8
  end
24
9
 
25
10
  test 'belongs_to' do
26
- issue = Issue.create
27
-
28
- record = TestObject.create(issue: issue)
29
-
30
- assert_equal issue, record.issue
31
- assert_equal issue.id, record.issue_id
32
-
33
- record = TestObject.find(record.id)
34
- assert_equal issue, record.issue
35
- end
36
-
37
- test 'belongs_to with class_name' do
38
- issue = Issue.create
39
-
40
- record = TestObject.create(widget: issue)
41
-
42
- assert_equal issue, record.widget
43
- assert_equal issue.id, record.widget_id
44
-
45
- record = TestObject.find(record.id)
46
- assert_equal issue, record.widget
47
- end
48
-
49
- test 'belongs_to with foreign_key' do
50
- issue = Issue.create
51
-
52
- record = TestObject.create(other_issue: issue)
53
-
54
- assert_equal issue, record.other_issue
55
- assert_equal issue.id, record.other_id
56
-
57
- record = TestObject.find(record.id)
58
- assert_equal issue, record.other_issue
59
- end
60
-
61
- test 'belongs_to with primary_key for ActiveRecord' do
62
- special_id = 'special_id'
63
- user = User.create! special_id: special_id
64
- record = TestObject.create user: user
65
-
66
- assert_equal user, record.user
67
- assert_equal special_id, record.user_id
68
-
69
- record = TestObject.find(record.id)
70
- assert_equal user, record.user
71
- end
72
-
73
- test 'belongs_to with polymorphic' do
74
- issue = Issue.create
75
-
76
- record = TestObject.create(target: issue)
11
+ user = User.create(special_id: 'abc')
12
+ issue = TestObject.create(user: user)
77
13
 
78
- assert_equal issue, record.target
79
- assert_equal issue.id, record.target_id
80
- assert_equal 'Issue', record.target_type
14
+ assert_equal user, issue.user
15
+ assert_equal issue.user_id, 'abc'
81
16
 
82
- record = TestObject.find(record.id)
83
- assert_equal issue, record.target
17
+ issue = TestObject.find(issue.id)
18
+ assert_equal user, issue.user
84
19
  end
85
20
 
86
21
  test 'belongs_to clear cache after reload' do
87
- issue = Issue.create
88
- record = TestObject.create(issue: issue)
89
- issue.destroy
22
+ user = User.create(special_id: 'abc')
23
+ issue = TestObject.create(user: user)
24
+ user.destroy
90
25
 
91
- assert_not_nil record.issue
92
- assert_nil TestObject.find(record.id).issue
93
- assert_nil record.reload.issue
26
+ assert_not_nil issue.user
27
+ assert_nil TestObject.find(issue.id).user
28
+ assert_nil issue.reload.user
94
29
  end
95
30
  end
@@ -1,12 +1,13 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class Superstore::Associations::ReflectionTest < Superstore::TestCase
4
- class ::Status < Superstore::Base; end
5
- class ::Job < Superstore::Base
6
- belongs_to :status
7
- end
8
-
9
- test 'class_name' do
10
- assert_equal 'Status', Job.new.association_reflections[:status].class_name
11
- end
4
+ # class ::Status < Superstore::Base; end
5
+ # class ::Job < Superstore::Base
6
+ # self.table_name = 'issues'
7
+ # belongs_to :status
8
+ # end
9
+ #
10
+ # test 'class_name' do
11
+ # assert_equal 'Status', Job.new.association_reflections[:status].class_name
12
+ # end
12
13
  end
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class Superstore::AttributeMethods::TypecastingTest < Superstore::TestCase
4
4
  class TestIssue < Superstore::Base
5
- self.table_name = 'Issues'
5
+ self.table_name = 'issues'
6
6
 
7
7
  boolean :enabled
8
8
  float :rating
@@ -78,7 +78,7 @@ class Superstore::AttributeMethods::TypecastingTest < Superstore::TestCase
78
78
 
79
79
  test 'multiple attributes definition' do
80
80
  class MultipleAttributesIssue < Superstore::Base
81
- self.table_name = 'Issues'
81
+ self.table_name = 'issues'
82
82
  end
83
83
 
84
84
  assert_nothing_raised {
@@ -91,7 +91,7 @@ class Superstore::AttributeMethods::TypecastingTest < Superstore::TestCase
91
91
 
92
92
  test 'multiple attributes with options' do
93
93
  class MultipleAttributesIssue < Superstore::Base
94
- self.table_name = 'Issues'
94
+ self.table_name = 'issues'
95
95
  end
96
96
 
97
97
  MultipleAttributesIssue.expects(:attribute).with(:hello, { :unique => :true, :type => :string })
@@ -12,6 +12,10 @@ class Superstore::AttributeMethodsTest < Superstore::TestCase
12
12
  assert_equal 'foo', issue.read_attribute(:description)
13
13
  end
14
14
 
15
+ test 'read primary_key' do
16
+ refute_nil Issue.new[:id]
17
+ end
18
+
15
19
  test 'hash accessor aliases' do
16
20
  issue = Issue.new
17
21
 
@@ -43,6 +47,7 @@ class Superstore::AttributeMethodsTest < Superstore::TestCase
43
47
  end
44
48
 
45
49
  class ReservedWord < Superstore::Base
50
+ self.table_name = 'issues'
46
51
  string :system
47
52
  end
48
53
 
@@ -14,7 +14,7 @@ class Superstore::BaseTest < Superstore::TestCase
14
14
  end
15
15
 
16
16
  test 'table_name' do
17
- assert_equal 'superstore_base_test_sons', Son.table_name
18
- assert_equal 'superstore_base_test_sons', Grandson.table_name
17
+ assert_equal 'sons', Son.table_name
18
+ assert_equal 'sons', Grandson.table_name
19
19
  end
20
20
  end
@@ -2,6 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class Superstore::CachingTest < Superstore::TestCase
4
4
  class ::OtherClass < Superstore::Base
5
+ self.table_name = 'issues'
5
6
  end
6
7
 
7
8
  test 'for a new record' do
@@ -2,7 +2,7 @@ require 'test_helper'
2
2
 
3
3
  class Superstore::CallbacksTest < Superstore::TestCase
4
4
  class TestIssue < Superstore::Base
5
- self.table_name = 'Issues'
5
+ self.table_name = 'issues'
6
6
  string :description
7
7
 
8
8
  %w(before_validation after_validation after_save after_create after_update after_destroy).each do |method|
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: superstore
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Koziarski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2016-02-12 00:00:00.000000000 Z
12
+ date: 2016-02-13 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel