active_record-acts_as 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/active_record/acts_as/querying.rb +10 -20
- data/lib/active_record/acts_as/relation.rb +0 -1
- data/lib/active_record/acts_as/version.rb +1 -1
- data/lib/active_record/acts_as.rb +2 -0
- data/spec/acts_as_spec.rb +5 -5
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4dd0c165fcfb62ca39a5698738d617bbed6a29e8
|
4
|
+
data.tar.gz: 1e4f6b5f4de2ac6c2bb2a0aa037c71e9dbdd692c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01980d25e52b92204c22b1588ba3d704be0f1f9814f96401bde32bb3152b3910e06c11036df3fca9a10167233081f30f911bc4d371546dea9363307b36ab5217
|
7
|
+
data.tar.gz: 0c10073d6a0d907ac594e76d55144aca04a15d4b709d535218e75f0795536b5886dcfe82cddc14fc0dd207d99ef49701bd85e0b6e7ee21836e1e8ceb025b3d2c
|
data/README.md
CHANGED
@@ -90,6 +90,10 @@ Pen.create name: 'Penie!', price: 0.8, color: 'red'
|
|
90
90
|
# => #<Pen id: 1, color: "red">
|
91
91
|
Pen.where price: 0.8
|
92
92
|
# => [#<Pen id: 1, color: "red">]
|
93
|
+
pen = Pen.where(name: 'new pen', color: 'black').first_or_initialize
|
94
|
+
# => #<Pen id: nil, color: "black">
|
95
|
+
pen.name
|
96
|
+
# => "new pen"
|
93
97
|
Product.where price: 0.8
|
94
98
|
# => [#<Product id: 1, name: "Penie!", price: 0.8, store_id: nil, actable_id: 1, actable_type: "Pen">]
|
95
99
|
pen = Pen.new
|
@@ -108,7 +112,7 @@ Product.first.specific
|
|
108
112
|
# => #<Pen ...>
|
109
113
|
```
|
110
114
|
|
111
|
-
In
|
115
|
+
In `has_many` case you can use subclasses:
|
112
116
|
|
113
117
|
```Ruby
|
114
118
|
store = Store.create
|
@@ -1,35 +1,25 @@
|
|
1
1
|
|
2
2
|
module ActiveRecord
|
3
|
-
module
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
opts[acting_as_model.table_name] = acts_as_opts
|
10
|
-
end
|
11
|
-
super(opts, *rest)
|
12
|
-
end
|
13
|
-
|
14
|
-
def find_by(*args)
|
15
|
-
where(*args).take
|
16
|
-
end
|
17
|
-
|
18
|
-
def find_by!(*args)
|
19
|
-
where(*args).take!
|
3
|
+
module QueryMethods
|
4
|
+
def where_with_acts_as(opts = :chain, *rest)
|
5
|
+
if acting_as? && opts.is_a?(Hash)
|
6
|
+
opts, acts_as_opts = opts.stringify_keys.partition { |k,v| attribute_method?(k) }
|
7
|
+
opts, acts_as_opts = Hash[opts], Hash[acts_as_opts]
|
8
|
+
opts[acting_as_model.table_name] = acts_as_opts unless acts_as_opts.empty?
|
20
9
|
end
|
10
|
+
where_without_acts_as(opts, *rest)
|
21
11
|
end
|
12
|
+
alias_method_chain :where, :acts_as
|
22
13
|
end
|
23
14
|
|
24
15
|
class Relation
|
25
|
-
|
26
|
-
|
27
|
-
def scope_for_create
|
16
|
+
def scope_for_create_with_acts_as
|
28
17
|
@scope_for_create ||= if acting_as?
|
29
18
|
where_values_hash.merge(where_values_hash(acting_as_model.table_name)).merge(create_with_value)
|
30
19
|
else
|
31
20
|
where_values_hash.merge(create_with_value)
|
32
21
|
end
|
33
22
|
end
|
23
|
+
alias_method_chain :scope_for_create, :acts_as
|
34
24
|
end
|
35
25
|
end
|
data/spec/acts_as_spec.rb
CHANGED
@@ -158,7 +158,7 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
|
|
158
158
|
context "Querying" do
|
159
159
|
before(:each) { clear_database }
|
160
160
|
|
161
|
-
it "
|
161
|
+
it "respects supermodel attributes in .where" do
|
162
162
|
red_pen = Pen.create(name: 'red pen', price: 0.8, color: 'red')
|
163
163
|
blue_pen = Pen.create(name: 'blue pen', price: 0.8, color: 'blue')
|
164
164
|
black_pen = Pen.create(name: 'black pen', price: 0.9, color: 'black')
|
@@ -168,7 +168,7 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
|
|
168
168
|
expect(Pen.where(price: 0.8)).to_not include(black_pen)
|
169
169
|
end
|
170
170
|
|
171
|
-
it "
|
171
|
+
it "respects supermodel attributes in .find_by" do
|
172
172
|
red_pen = Pen.create(name: 'red pen', price: 0.8, color: 'red')
|
173
173
|
blue_pen = Pen.create(name: 'blue pen', price: 0.8, color: 'blue')
|
174
174
|
black_pen = Pen.create(name: 'black pen', price: 0.9, color: 'black')
|
@@ -179,9 +179,9 @@ RSpec.describe "ActiveRecord::Base model with #acts_as called" do
|
|
179
179
|
end
|
180
180
|
|
181
181
|
it "includes supermodel attributes in Relation.scope_for_create" do
|
182
|
-
relation = Pen.where(name: 'new name')
|
183
|
-
expect(relation.scope_for_create.keys).to include(
|
184
|
-
expect(relation.scope_for_create[
|
182
|
+
relation = Pen.where(name: 'new name', price: 1.4, color: 'red')
|
183
|
+
expect(relation.scope_for_create.keys).to include('name')
|
184
|
+
expect(relation.scope_for_create['name']).to eq('new name')
|
185
185
|
end
|
186
186
|
end
|
187
187
|
|
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: 1.0.
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hassan Zamani
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sqlite3
|