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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 3d5548c95a54d8cbe26c5714273ec3ca3a3bf0ff
4
- data.tar.gz: 70a127389d745b09cf38eba99f120cc9fa42e692
3
+ metadata.gz: 4dd0c165fcfb62ca39a5698738d617bbed6a29e8
4
+ data.tar.gz: 1e4f6b5f4de2ac6c2bb2a0aa037c71e9dbdd692c
5
5
  SHA512:
6
- metadata.gz: 18d8183369e11b5b136f76e2b94d3d4ffad186113f1266031a92f6960abea9a45577ab5e718a5130b4b609939ea62f49f4993e0df777cf646d669ac074f0795c
7
- data.tar.gz: cf442a282239a0a618dbf78b10ff234286537ba38fb2a5c78b098d9b3754ec268591405e9d4fbf6ee8eb2f5a20c9068fce9d922c28596227138012afb8835f71
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 +has_many+ case you can use subclasses:
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 ActsAs
4
- module Querying
5
- def where(opts = :chain, *rest)
6
- if opts.is_a? Hash
7
- opts, acts_as_opts = opts.partition { |k,v| attribute_names.include?(k.to_s) }
8
- opts, acts_as_opts = Hash[opts], Hash[acts_as_opts]
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
- alias_method :scope_for_create_without_acting_as, :scope_for_create
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
@@ -20,7 +20,6 @@ module ActiveRecord
20
20
  alias_method :acting_as=, "#{name}=".to_sym
21
21
 
22
22
  include ActsAs::InstanceMethods
23
- extend ActsAs::Querying
24
23
  end
25
24
 
26
25
  def acting_as?(other = nil)
@@ -1,6 +1,6 @@
1
1
 
2
2
  module ActiveRecord
3
3
  module ActsAs
4
- VERSION = "1.0.0"
4
+ VERSION = "1.0.1"
5
5
  end
6
6
  end
@@ -1,3 +1,5 @@
1
+ require 'active_support'
2
+ require 'active_record'
1
3
  require 'active_record/acts_as/version'
2
4
  require 'active_record/acts_as/relation'
3
5
  require 'active_record/acts_as/migration'
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 ".where works with supermodel attributes" do
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 "find_by works with supermodel attributes" do
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(:name)
184
- expect(relation.scope_for_create[:name]).to eq('new name')
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.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-03 00:00:00.000000000 Z
11
+ date: 2014-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sqlite3