active_record-acts_as 1.0.8 → 2.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/.travis.yml +5 -5
- data/Appraisals +7 -0
- data/README.md +3 -1
- data/active_record-acts_as.gemspec +5 -4
- data/gemfiles/rails_4.2.gemfile +8 -0
- data/gemfiles/rails_5.0.gemfile +8 -0
- data/lib/active_record/acts_as/class_methods.rb +8 -6
- data/lib/active_record/acts_as/migration.rb +1 -1
- data/lib/active_record/acts_as/querying.rb +22 -17
- data/lib/active_record/acts_as/relation.rb +11 -1
- data/lib/active_record/acts_as/version.rb +1 -1
- data/spec/acts_as_spec.rb +50 -0
- data/spec/migrations_spec.rb +57 -2
- data/spec/models.rb +1 -1
- metadata +28 -16
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e23d681d9272dad587e888164a0a1699cac86717
|
4
|
+
data.tar.gz: e2fca48672513bae073e702cfdd5631241f1b6d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 54e3c3e84139c8ac514e13366cb02d3411a1cd9becbe4bbed6b6aa9b3045151a03f8dde9a7c29d287d82972f6c52e4fe8ff7860c6d94cef21d4f09d8e15af295
|
7
|
+
data.tar.gz: 323da76a14e32627d7b52395a3244c87e813fa6df650df302983e0ca51ed815c81129cdf2e57ba5a47f3f7b09cd45bfce37c196c29dd0ba91f784b5762b93201
|
data/.gitignore
CHANGED
data/.travis.yml
CHANGED
data/Appraisals
ADDED
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# coding: utf-8
|
2
1
|
lib = File.expand_path('../lib', __FILE__)
|
3
2
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
|
4
4
|
require 'active_record/acts_as/version'
|
5
5
|
|
6
6
|
Gem::Specification.new do |spec|
|
@@ -18,13 +18,14 @@ Gem::Specification.new do |spec|
|
|
18
18
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
|
-
spec.required_ruby_version = ">=
|
21
|
+
spec.required_ruby_version = ">= 2.2"
|
22
22
|
|
23
23
|
spec.add_development_dependency "sqlite3", "~> 1.3"
|
24
24
|
spec.add_development_dependency "bundler", "~> 1.6"
|
25
25
|
spec.add_development_dependency "rspec", "~> 3"
|
26
26
|
spec.add_development_dependency "rake", "~> 10"
|
27
|
+
spec.add_development_dependency "appraisal", "~> 2.1"
|
27
28
|
|
28
|
-
spec.add_dependency "activesupport", "
|
29
|
-
spec.add_dependency "activerecord", "
|
29
|
+
spec.add_dependency "activesupport", ">= 4.2"
|
30
|
+
spec.add_dependency "activerecord", ">= 4.2"
|
30
31
|
end
|
@@ -1,13 +1,15 @@
|
|
1
1
|
module ActiveRecord
|
2
2
|
module ActsAs
|
3
|
-
module
|
4
|
-
def
|
5
|
-
|
3
|
+
module ReflectionsWithActsAs
|
4
|
+
def _reflections
|
5
|
+
@_reflections_acts_as_cache ||=
|
6
|
+
super.reverse_merge(acting_as_model._reflections)
|
6
7
|
end
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
module ClassMethods
|
11
|
+
def self.included(module_)
|
12
|
+
module_.prepend ReflectionsWithActsAs
|
11
13
|
end
|
12
14
|
|
13
15
|
def validators_on(*args)
|
@@ -1,25 +1,30 @@
|
|
1
|
-
|
2
1
|
module ActiveRecord
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
2
|
+
module ActsAs
|
3
|
+
module QueryMethods
|
4
|
+
def where(opts = :chain, *rest)
|
5
|
+
if acting_as? && opts.is_a?(Hash)
|
6
|
+
opts = opts.merge(opts.delete(klass.table_name) || {})
|
7
|
+
|
8
|
+
opts, acts_as_opts = opts.stringify_keys.partition { |k,v| attribute_method?(k) }
|
9
|
+
opts, acts_as_opts = Hash[opts], Hash[acts_as_opts]
|
10
|
+
opts[acting_as_model.table_name] = acts_as_opts unless acts_as_opts.empty?
|
11
|
+
end
|
12
|
+
|
13
|
+
super
|
9
14
|
end
|
10
|
-
where_without_acts_as(opts, *rest)
|
11
15
|
end
|
12
|
-
alias_method_chain :where, :acts_as
|
13
|
-
end
|
14
16
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
17
|
+
module ScopeForCreate
|
18
|
+
def scope_for_create
|
19
|
+
@scope_for_create ||= if acting_as?
|
20
|
+
where_values_hash.merge(where_values_hash(acting_as_model.table_name)).merge(create_with_value)
|
21
|
+
else
|
22
|
+
where_values_hash.merge(create_with_value)
|
23
|
+
end
|
21
24
|
end
|
22
25
|
end
|
23
|
-
alias_method_chain :scope_for_create, :acts_as
|
24
26
|
end
|
27
|
+
|
28
|
+
Relation.send(:prepend, ActsAs::QueryMethods)
|
29
|
+
Relation.send(:prepend, ActsAs::ScopeForCreate)
|
25
30
|
end
|
@@ -7,12 +7,22 @@ module ActiveRecord
|
|
7
7
|
module ClassMethods
|
8
8
|
def acts_as(name, scope = nil, options = {})
|
9
9
|
options, scope = scope, nil if Hash === scope
|
10
|
+
association_method = options.delete(:association_method)
|
10
11
|
options = {as: :actable, dependent: :destroy, validate: false, autosave: true}.merge options
|
11
12
|
|
12
13
|
cattr_reader(:validates_actable) { options.delete(:validates_actable) == false ? false : true }
|
13
14
|
|
14
15
|
reflections = has_one name, scope, options
|
15
|
-
default_scope -> {
|
16
|
+
default_scope -> {
|
17
|
+
case association_method
|
18
|
+
when :eager_load
|
19
|
+
eager_load(name)
|
20
|
+
when :joins
|
21
|
+
joins(name)
|
22
|
+
else
|
23
|
+
includes(name)
|
24
|
+
end
|
25
|
+
}
|
16
26
|
validate :actable_must_be_valid
|
17
27
|
after_update :touch_actable
|
18
28
|
|
data/spec/acts_as_spec.rb
CHANGED
@@ -349,4 +349,54 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
|
|
349
349
|
expect(subject.acting_as_model).to eq Inventory::ProductFeature
|
350
350
|
end
|
351
351
|
end
|
352
|
+
|
353
|
+
context 'different association_methods' do
|
354
|
+
before(:each) do
|
355
|
+
Object.send(:remove_const, :Pen)
|
356
|
+
end
|
357
|
+
|
358
|
+
it "should not include the selected attribute when associating using 'eager_load'" do
|
359
|
+
class Pen < ActiveRecord::Base
|
360
|
+
acts_as :product , {association_method: :eager_load}
|
361
|
+
store_accessor :settings, :option1
|
362
|
+
validates_presence_of :color
|
363
|
+
end
|
364
|
+
Pen.create pen_attributes
|
365
|
+
|
366
|
+
expect(Pen.select("'something' as thing").first['thing']).to be_nil
|
367
|
+
end
|
368
|
+
|
369
|
+
it "should include the selected attribute in the model when associating using 'includes'" do
|
370
|
+
class Pen < ActiveRecord::Base
|
371
|
+
acts_as :product , {association_method: :includes}
|
372
|
+
store_accessor :settings, :option1
|
373
|
+
validates_presence_of :color
|
374
|
+
end
|
375
|
+
Pen.create pen_attributes
|
376
|
+
|
377
|
+
expect(Pen.select("'something' as thing").first['thing']).to eq 'something'
|
378
|
+
end
|
379
|
+
|
380
|
+
it "should include the selected attribute in the model not specifying an association_method" do
|
381
|
+
class Pen < ActiveRecord::Base
|
382
|
+
acts_as :product
|
383
|
+
store_accessor :settings, :option1
|
384
|
+
validates_presence_of :color
|
385
|
+
end
|
386
|
+
Pen.create pen_attributes
|
387
|
+
|
388
|
+
expect(Pen.select("'something' as thing").first['thing']).to eq 'something'
|
389
|
+
end
|
390
|
+
|
391
|
+
it "should include a selected attribute from the parent when associating using 'joins'" do
|
392
|
+
class Pen < ActiveRecord::Base
|
393
|
+
acts_as :product, {association_method: :joins}
|
394
|
+
store_accessor :settings, :option1
|
395
|
+
validates_presence_of :color
|
396
|
+
end
|
397
|
+
Pen.create pen_attributes
|
398
|
+
|
399
|
+
expect(Pen.select("price as thing").first['thing']).to eq 0.8
|
400
|
+
end
|
401
|
+
end
|
352
402
|
end
|
data/spec/migrations_spec.rb
CHANGED
@@ -5,22 +5,77 @@ require 'active_record/acts_as'
|
|
5
5
|
class Product < ActiveRecord::Base
|
6
6
|
end
|
7
7
|
|
8
|
+
class RemoveActableFromProducts < ActiveRecord::Migration
|
9
|
+
def change
|
10
|
+
change_table(:products) do |t|
|
11
|
+
t.remove_actable
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class RemoveProduceableFromProducts < ActiveRecord::Migration
|
17
|
+
def change
|
18
|
+
change_table(:products) do |t|
|
19
|
+
t.remove_actable(as: :produceable)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
8
23
|
|
9
24
|
RSpec.describe ".actable" do
|
10
25
|
context "in .create_table block" do
|
11
26
|
after { initialize_schema }
|
12
27
|
context "with :as options" do
|
13
|
-
it "creates
|
28
|
+
it "creates polymorphic reference columns with given name" do
|
14
29
|
initialize_database { create_table(:products) { |t| t.actable(as: :produceable) } }
|
15
30
|
expect(Product.column_names).to include('produceable_id', 'produceable_type')
|
16
31
|
end
|
17
32
|
end
|
18
33
|
|
19
34
|
context "with no args" do
|
20
|
-
it "creates
|
35
|
+
it "creates polymorphic reference columns" do
|
21
36
|
initialize_database { create_table(:products) { |t| t.actable } }
|
22
37
|
expect(Product.column_names).to include('actable_id', 'actable_type')
|
23
38
|
end
|
24
39
|
end
|
25
40
|
end
|
26
41
|
end
|
42
|
+
|
43
|
+
RSpec.describe ".remove_actable" do
|
44
|
+
context "in .modify_table block" do
|
45
|
+
after { initialize_schema }
|
46
|
+
|
47
|
+
context "with :as options" do
|
48
|
+
before do
|
49
|
+
initialize_database do
|
50
|
+
create_table(:products) do |t|
|
51
|
+
t.string :name
|
52
|
+
t.actable(as: :produceable)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
it "removes polymorphic reference columns with given name" do
|
57
|
+
mig = RemoveProduceableFromProducts.new
|
58
|
+
mig.exec_migration(ActiveRecord::Base.connection, :up)
|
59
|
+
Product.reset_column_information
|
60
|
+
expect(Product.column_names).not_to include('produceable_id', 'produceable_type')
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "with no args" do
|
65
|
+
before do
|
66
|
+
initialize_database do
|
67
|
+
create_table(:products) do |t|
|
68
|
+
t.string :name
|
69
|
+
t.actable
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
it "creates polymorphic reference columns" do
|
74
|
+
mig = RemoveActableFromProducts.new
|
75
|
+
mig.exec_migration(ActiveRecord::Base.connection, :up)
|
76
|
+
Product.reset_column_information
|
77
|
+
expect(Product.column_names).not_to include('actable_id', 'actable_type')
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
data/spec/models.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-acts_as
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 2.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hassan Zamani
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-09-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|
@@ -67,39 +67,47 @@ dependencies:
|
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '10'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: appraisal
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
76
|
-
type: :
|
75
|
+
version: '2.1'
|
76
|
+
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '2.1'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
|
-
name:
|
84
|
+
name: activesupport
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "~>"
|
88
|
-
- !ruby/object:Gem::Version
|
89
|
-
version: '4'
|
90
87
|
- - ">="
|
91
88
|
- !ruby/object:Gem::Version
|
92
|
-
version: 4.
|
89
|
+
version: '4.2'
|
93
90
|
type: :runtime
|
94
91
|
prerelease: false
|
95
92
|
version_requirements: !ruby/object:Gem::Requirement
|
96
93
|
requirements:
|
97
|
-
- - "
|
94
|
+
- - ">="
|
98
95
|
- !ruby/object:Gem::Version
|
99
|
-
version: '4'
|
96
|
+
version: '4.2'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: activerecord
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '4.2'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
100
108
|
- - ">="
|
101
109
|
- !ruby/object:Gem::Version
|
102
|
-
version: 4.
|
110
|
+
version: '4.2'
|
103
111
|
description: Simulate multi-table inheritance for activerecord models using a plymorphic
|
104
112
|
association
|
105
113
|
email:
|
@@ -111,11 +119,14 @@ files:
|
|
111
119
|
- ".gitignore"
|
112
120
|
- ".rspec"
|
113
121
|
- ".travis.yml"
|
122
|
+
- Appraisals
|
114
123
|
- Gemfile
|
115
124
|
- LICENSE.txt
|
116
125
|
- README.md
|
117
126
|
- Rakefile
|
118
127
|
- active_record-acts_as.gemspec
|
128
|
+
- gemfiles/rails_4.2.gemfile
|
129
|
+
- gemfiles/rails_5.0.gemfile
|
119
130
|
- lib/active_record/acts_as.rb
|
120
131
|
- lib/active_record/acts_as/class_methods.rb
|
121
132
|
- lib/active_record/acts_as/instance_methods.rb
|
@@ -144,7 +155,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
155
|
requirements:
|
145
156
|
- - ">="
|
146
157
|
- !ruby/object:Gem::Version
|
147
|
-
version: '
|
158
|
+
version: '2.2'
|
148
159
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
149
160
|
requirements:
|
150
161
|
- - ">="
|
@@ -152,7 +163,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
152
163
|
version: '0'
|
153
164
|
requirements: []
|
154
165
|
rubyforge_project:
|
155
|
-
rubygems_version: 2.
|
166
|
+
rubygems_version: 2.6.6
|
156
167
|
signing_key:
|
157
168
|
specification_version: 4
|
158
169
|
summary: Simulate multi-table inheritance for activerecord models
|
@@ -165,3 +176,4 @@ test_files:
|
|
165
176
|
- spec/models.rb
|
166
177
|
- spec/rspec_matchers_spec.rb
|
167
178
|
- spec/spec_helper.rb
|
179
|
+
has_rdoc:
|