Skip to content

Commit

Permalink
Merge pull request #35 from chancedowns/master
Browse files Browse the repository at this point in the history
Add support for ActiveRecord::Store
  • Loading branch information
hzamani committed May 16, 2015
2 parents 819586a + e1010a7 commit c9f6544
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
18 changes: 17 additions & 1 deletion lib/active_record/acts_as/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,23 @@ def write_attribute(attr_name, value, *args, &block)
acting_as.send(:write_attribute, attr_name, value, *args, &block)
end
end
private :write_attribute

def read_store_attribute(store_attribute, key)
if attribute_method?(store_attribute.to_s)
super
else
acting_as.read_store_attribute(store_attribute, key)
end
end

def write_store_attribute(store_attribute, key, value)
if attribute_method?(store_attribute.to_s)
super
else
acting_as.send(:write_store_attribute, store_attribute, key, value)
end
end
private :write_attribute, :write_store_attribute

def attributes
acting_as_persisted? ? acting_as.attributes.except(acting_as_reflection.type, acting_as_reflection.foreign_key).merge(super) : super
Expand Down
29 changes: 28 additions & 1 deletion spec/acts_as_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,33 @@
expect(pen.present).to eq("pen - $0.8")
end

it 'responds to serialized attribute' do
expect(pen).to respond_to('option1')
expect(isolated_pen).to respond_to('option2')
end

it 'responds to supermodel serialized attribute' do
expect(pen).to respond_to('global_option')
expect(isolated_pen).to respond_to('global_option')
end

it 'does not respond to other models serialized attribute' do
expect(pen).to_not respond_to('option2')
expect(isolated_pen).to_not respond_to('option1')
end

it 'saves supermodel serialized attribute on save' do
pen.option1 = 'value1'
pen.global_option = 'globalvalue'
pen.save
pen.reload
isolated_pen.save
isolated_pen.reload
expect(pen.option1).to eq('value1')
expect(isolated_pen).to_not respond_to('option1')
expect(JSON.parse(pen.to_json)).to eq(JSON.parse('{"id":' + pen.id.to_s + ',"name":"pen","price":0.8,"store_id":null,"settings":{"global_option":"globalvalue","option1":"value1"},"color":"red"}'))
end

it "saves supermodel attributes on save" do
pen.save
pen.reload
Expand Down Expand Up @@ -185,7 +212,7 @@
it "if the submodel instance association exists" do
p = Product.new(name: 'Test Pen', price: 0.8, actable: pen)
p.save
expect(JSON.parse(pen.to_json)).to eq(JSON.parse('{"id":' + pen.id.to_s + ',"name":"pen","price":0.8,"store_id":null,"color":"red"}'))
expect(JSON.parse(pen.to_json)).to eq(JSON.parse('{"id":' + pen.id.to_s + ',"name":"pen","price":0.8,"store_id":null,"settings": {},"color":"red"}'))
end
end

Expand Down
4 changes: 4 additions & 0 deletions spec/models.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Product < ActiveRecord::Base
actable
belongs_to :store
validates_presence_of :name, :price
store :settings, accessors: [:global_option]

def present
"#{name} - $#{price}"
Expand All @@ -17,13 +18,15 @@ def raise_error

class Pen < ActiveRecord::Base
acts_as :product
store_accessor :settings, :option1

validates_presence_of :color
end

class IsolatedPen < ActiveRecord::Base
self.table_name = :pens
acts_as :product, validates_actable: false
store_accessor :settings, :option2

validates_presence_of :color
end
Expand Down Expand Up @@ -60,6 +63,7 @@ class PenLid < ActiveRecord::Base
t.string :name
t.float :price
t.integer :store_id
t.text :settings
t.actable
end

Expand Down

0 comments on commit c9f6544

Please sign in to comment.