Skip to content

Commit

Permalink
Started using class variable so that sub classes can inherit hstore m…
Browse files Browse the repository at this point in the history
…etadata
  • Loading branch information
Michael Crismali committed Feb 8, 2015
1 parent e0f535b commit a494685
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 8 deletions.
10 changes: 5 additions & 5 deletions lib/hstore_accessor/macro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module HstoreAccessor
module Macro
module ClassMethods
def hstore_accessor(hstore_attribute, fields)
@hstore_keys_and_types ||= {}
@@hstore_keys_and_types ||= {}

"hstore_metadata_for_#{hstore_attribute}".tap do |method_name|
singleton_class.send(:define_method, method_name) do
Expand All @@ -16,7 +16,7 @@ def hstore_accessor(hstore_attribute, fields)

if ActiveRecord::VERSION::STRING.to_f >= 4.2
singleton_class.send(:define_method, :type_for_attribute) do |attribute|
data_type = @hstore_keys_and_types[attribute]
data_type = @@hstore_keys_and_types[attribute]
if data_type
TypeHelpers::TYPES[data_type].new
else
Expand All @@ -25,7 +25,7 @@ def hstore_accessor(hstore_attribute, fields)
end

singleton_class.send(:define_method, :column_for_attribute) do |attribute|
data_type = @hstore_keys_and_types[attribute.to_s]
data_type = @@hstore_keys_and_types[attribute.to_s]
if data_type
TypeHelpers.column_type_for(attribute.to_s, data_type)
else
Expand All @@ -34,7 +34,7 @@ def hstore_accessor(hstore_attribute, fields)
end
else
field_methods.send(:define_method, :column_for_attribute) do |attribute|
data_type = self.class.instance_eval { @hstore_keys_and_types }[attribute.to_s]
data_type = @@hstore_keys_and_types[attribute.to_s]
if data_type
TypeHelpers.column_type_for(attribute.to_s, data_type)
else
Expand All @@ -57,7 +57,7 @@ def hstore_accessor(hstore_attribute, fields)

raise Serialization::InvalidDataTypeError unless Serialization::VALID_TYPES.include?(data_type)

@hstore_keys_and_types[key.to_s] = data_type
@@hstore_keys_and_types[key.to_s] = data_type

field_methods.instance_eval do
define_method("#{key}=") do |value|
Expand Down
9 changes: 6 additions & 3 deletions spec/hstore_accessor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ class Product < ActiveRecord::Base
hstore_accessor :data, DATA_FIELDS
end

class SuperProduct < Product
end

describe HstoreAccessor do
context "macro" do
let(:product) { Product.new }
Expand Down Expand Up @@ -274,7 +277,7 @@ class FakeModel

describe "#type_for_attribute" do
if ::ActiveRecord::VERSION::STRING.to_f >= 4.2
subject { Product }
subject { SuperProduct }

def self.it_returns_the_type_for_the_attribute(type, attribute_name, active_record_type)
context "#{type}" do
Expand Down Expand Up @@ -305,7 +308,7 @@ def self.it_returns_the_type_for_the_attribute(type, attribute_name, active_reco

def self.it_returns_the_properly_typed_column(type, attribute_name, cast_type_class)
context "#{type}" do
subject { Product.column_for_attribute(attribute_name) }
subject { SuperProduct.column_for_attribute(attribute_name) }
it "returns a column with a #{type} cast type" do
expect(subject).to be_a(ActiveRecord::ConnectionAdapters::Column)
expect(subject.cast_type).to eq(cast_type_class.new)
Expand All @@ -325,7 +328,7 @@ def self.it_returns_the_properly_typed_column(type, attribute_name, cast_type_cl
else
def self.it_returns_the_properly_typed_column(hstore_type, attribute_name, active_record_type)
context "#{hstore_type}" do
subject { Product.new.column_for_attribute(attribute_name) }
subject { SuperProduct.new.column_for_attribute(attribute_name) }
it "returns a column with a #{hstore_type} cast type" do
expect(subject).to be_a(ActiveRecord::ConnectionAdapters::Column)
expect(subject.type).to eq(active_record_type)
Expand Down

0 comments on commit a494685

Please sign in to comment.