simple_eav 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/lib/simple_eav.rb CHANGED
@@ -16,10 +16,24 @@ module SimpleEav
16
16
  self.class.simple_eav_column
17
17
  end
18
18
 
19
+ private
20
+ def associations_of_class
21
+ self.class.reflect_on_all_associations.map{|assoc| assoc.name.to_sym }
22
+ end
23
+
19
24
  def actual_columns_of_table
20
25
  self.class.columns.map{|col| col.name.to_sym }
21
26
  end
22
27
 
28
+ public
29
+ def reserved_attributes
30
+ associations_of_class + actual_columns_of_table
31
+ end
32
+
33
+ def reserved_attribute?(attribute)
34
+ reserved_attributes.include?(attribute)
35
+ end
36
+
23
37
  def simple_eav_attributes
24
38
  _attributes = self.send(simple_eav_column.to_sym)
25
39
  _attributes.is_a?(Hash) ? _attributes : {}
@@ -35,7 +49,7 @@ module SimpleEav
35
49
  # - remove undefined columns to prevent UnknownAttribute::Error from being thrown
36
50
  simple_eav_attrs = read_attribute(simple_eav_column.to_sym) || {}
37
51
  _attributes.each do |column,value|
38
- next if actual_columns_of_table.include?(column)
52
+ next if reserved_attribute?(column.to_sym)
39
53
  simple_eav_attrs[column] = value
40
54
  _attributes.delete(column)
41
55
  end
@@ -44,7 +58,8 @@ module SimpleEav
44
58
  end
45
59
 
46
60
  def respond_to?(method, include_private=false)
47
- super || simple_eav_attributes.has_key?(method)
61
+ return true if super(method, include_private)
62
+ simple_eav_attributes.has_key?(method)
48
63
  end
49
64
 
50
65
  def method_missing(method, *args, &block)
data/lib/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module SimpleEav
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
data/spec/db/schema.rb CHANGED
@@ -7,4 +7,9 @@ ActiveRecord::Schema.define do
7
7
  t.datetime 'created_at'
8
8
  t.datetime 'updated_at'
9
9
  end
10
+
11
+ create_table 'children', :force => true do |t|
12
+ t.string 'name'
13
+ t.belongs_to :person
14
+ end
10
15
  end
@@ -12,10 +12,61 @@ describe SimpleEav do
12
12
  end
13
13
  end
14
14
 
15
+ describe "Reserved attributes" do
16
+ before(:each) do
17
+ @child = Child.create
18
+ @person = Person.create(:child=>@child)
19
+ end
20
+ it "knows the associations of the object" do
21
+ @person.send(:associations_of_class).should include(:child)
22
+ end
23
+ it "knows the reserved attributes" do
24
+ @person.should_receive(:associations_of_class).and_return([:child])
25
+ @person.should_receive(:actual_columns_of_table).and_return([:name])
26
+ @person.reserved_attributes.should eql([:child, :name])
27
+ end
28
+ it "does not accept reserved attributes for eav" do
29
+ @person.should_receive(:reserved_attributes).and_return([:name, :number])
30
+ @person.reserved_attribute?(:name).should be_true
31
+ end
32
+ end
33
+
15
34
  describe "Expected ActiveRecord behavior" do
16
- it "handles an empty string of attributes" do
17
- person = Person.create(:simple_attributes=>'')
18
- person.should_not be_new_record
35
+ describe "common" do
36
+ it "handles an empty string of attributes" do
37
+ person = Person.create(:simple_attributes=>'')
38
+ person.should_not be_new_record
39
+ end
40
+ it "sets all of the attributes" do
41
+ person = Person.create!({
42
+ :age=>99,
43
+ :simple_attributes=>{
44
+ :name=>'John'
45
+ }
46
+ })
47
+ person.age.should eql(99)
48
+ person.name.should eql('John')
49
+ end
50
+ end
51
+ describe "associations" do
52
+ before(:each) do
53
+ @child = Child.create
54
+ @person = Person.create(:child=>@child)
55
+ end
56
+ it "assigns the has_one" do
57
+ @person.simple_eav_attributes.should_not have_key(:child)
58
+ @person.child.should eql(@child)
59
+ end
60
+ end
61
+ describe "serialization" do
62
+ it "serializes and deserializes the simple_eav attributes" do
63
+ person = Person.new({:simple_attributes=>{
64
+ :name=>'John'
65
+ }})
66
+ person.save!
67
+ person.reload
68
+ person.name.should eql('John')
69
+ end
19
70
  end
20
71
  describe "given a hash of undefined attributes" do
21
72
  it "creates a person with a name" do
@@ -52,24 +103,6 @@ describe SimpleEav do
52
103
  person.simple_eav_attributes.should have_key :new_age
53
104
  end
54
105
  end
55
- it "sets all of the attributes" do
56
- person = Person.create!({
57
- :age=>99,
58
- :simple_attributes=>{
59
- :name=>'John'
60
- }
61
- })
62
- person.age.should eql(99)
63
- person.name.should eql('John')
64
- end
65
- it "serializes and deserializes the simple_eav attributes" do
66
- person = Person.new({:simple_attributes=>{
67
- :name=>'John'
68
- }})
69
- person.save!
70
- person.reload
71
- person.name.should eql('John')
72
- end
73
106
  end
74
107
 
75
108
  describe "#method_missing" do
@@ -79,6 +112,25 @@ describe SimpleEav do
79
112
  end
80
113
  end
81
114
 
115
+ describe "#respond_to" do
116
+ before(:each) do
117
+ @person = Person.new
118
+ end
119
+
120
+ it "the defined attribute `age` is responded to" do
121
+ @person.respond_to?(:age).should be_true
122
+ end
123
+
124
+ it "the custome attribute `name` is responded to" do
125
+ @person.name = 'John'
126
+ @person.respond_to?(:name).should be_true
127
+ end
128
+
129
+ it "someone else's name is not responded to" do
130
+ @person.respond_to?(:someone_elses_name).should be_false
131
+ end
132
+ end
133
+
82
134
  describe "A missing attribute" do
83
135
  before( :each ) do
84
136
  @person = Person.new
@@ -122,15 +174,6 @@ describe SimpleEav do
122
174
  @person.name = 'Joe'
123
175
  @person.name.should eql('Joe')
124
176
  end
125
-
126
- it "who responds to his name" do
127
- @person.name = 'John'
128
- @person.respond_to?(:name).should be_true
129
- end
130
-
131
- it "who doesn't respond to someone else's name" do
132
- @person.respond_to?(:someone_elses_name).should be_false
133
- end
134
177
  end
135
178
  end
136
179
  end
data/spec/spec_helper.rb CHANGED
@@ -16,6 +16,7 @@ load('db/schema.rb')
16
16
 
17
17
  $:.push File.expand_path("../lib", __FILE__)
18
18
  require 'support/person'
19
+ require 'support/child'
19
20
 
20
21
  RSpec.configure do |config|
21
22
  config.mock_with :rspec
@@ -0,0 +1,3 @@
1
+ class Child < ActiveRecord::Base
2
+ belongs_to :person
3
+ end
@@ -3,6 +3,8 @@ require 'simple_eav'
3
3
 
4
4
  class Person < ActiveRecord::Base
5
5
  include SimpleEav
6
-
7
6
  configure_simple_eav :simple_attributes
7
+
8
+ has_one :child
8
9
  end
10
+
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: simple_eav
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.3.0
5
+ version: 0.4.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Tim Linquist
@@ -10,7 +10,8 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-07-30 00:00:00 Z
13
+ date: 2011-08-03 00:00:00 -07:00
14
+ default_executable:
14
15
  dependencies:
15
16
  - !ruby/object:Gem::Dependency
16
17
  name: activerecord
@@ -78,7 +79,9 @@ files:
78
79
  - spec/db/schema.rb
79
80
  - spec/lib/simple_eav_spec.rb
80
81
  - spec/spec_helper.rb
82
+ - spec/support/child.rb
81
83
  - spec/support/person.rb
84
+ has_rdoc: true
82
85
  homepage: http://github.com/timo3377/simple_eav
83
86
  licenses: []
84
87
 
@@ -102,7 +105,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
102
105
  requirements: []
103
106
 
104
107
  rubyforge_project:
105
- rubygems_version: 1.8.3
108
+ rubygems_version: 1.5.0
106
109
  signing_key:
107
110
  specification_version: 3
108
111
  summary: A simple alternative to acts_as_eav_model.
@@ -110,4 +113,5 @@ test_files:
110
113
  - spec/db/schema.rb
111
114
  - spec/lib/simple_eav_spec.rb
112
115
  - spec/spec_helper.rb
116
+ - spec/support/child.rb
113
117
  - spec/support/person.rb