simple_eav 0.0.1 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +7 -16
- data/lib/simple_eav.rb +18 -0
- data/lib/version.rb +1 -1
- data/simple_eav.gemspec +1 -1
- data/spec/lib/simple_eav_spec.rb +39 -4
- metadata +6 -4
data/README.rdoc
CHANGED
@@ -3,22 +3,11 @@
|
|
3
3
|
simple_eav provides a more simple alternative to {acts_as_eav_model}[https://github.com/visfleet/acts_as_eav_model] that works with
|
4
4
|
ActiveRecord without any monkey patching. This gem is designed to be a replacement for acts_as_eav_model.
|
5
5
|
|
6
|
-
Acts_as_eav_model's purpose is to provide a model with any number of custom attributes. This project has the same purpose. The difference being maintaining utmost
|
7
|
-
compatibility with ActiveRecord::Base. For this reason, there is one glaring inconsistency between this gem and acts_as_eav_model.
|
8
|
-
Acts_as_eav_model overrides attributes (among other functionality) to allow you to set any custom attributes. Simple eav does not. I ommitted this in the interest
|
9
|
-
of not having to monkey patch ActiveRecord.
|
6
|
+
Acts_as_eav_model's purpose is to provide a model with any number of custom attributes. This project has the same purpose. The difference being maintaining utmost compatibility with ActiveRecord::Base.
|
10
7
|
|
11
|
-
|
12
|
-
person = Person.new {:custom_attribute=>'value'} #This does not work in simple_eav
|
8
|
+
Version 0.1.0 makes this library, to my knowledge, fully compatible with acts_as_eav_model. This update introduced the ability to specify custom attributes exactly the same as attributes with an actual column in the database ie:
|
13
9
|
|
14
|
-
|
15
|
-
person.custom_attribute ~> 'value'
|
16
|
-
|
17
|
-
The latter works because all simple_eav does is use ActiveRecord's built in serialize mechanism to store a hash of custom attributes.
|
18
|
-
So in this example, simple_eav_attributes is a text column in the database for the persons table. Simple eav then takes of
|
19
|
-
serializing the data for this column and defines setters/getters for your custom values.
|
20
|
-
|
21
|
-
This is the only known inconsistency between these two gems. If this is an issue in your project please let me know.
|
10
|
+
Person.create :attribute_with_column=>true, :attribute_without_column=>true
|
22
11
|
|
23
12
|
== Installation
|
24
13
|
gem install simple_eav
|
@@ -43,6 +32,10 @@ This is the only known inconsistency between these two gems. If this is an issue
|
|
43
32
|
person = Person.new
|
44
33
|
person.name = 'Joe'
|
45
34
|
person.name ~> 'Joe'
|
35
|
+
|
36
|
+
person = Person.new :name => 'Joe'
|
37
|
+
person.save!
|
38
|
+
person.name ~> 'Joe'
|
46
39
|
|
47
40
|
== Migrating to simple_eav
|
48
41
|
I have not done this yet but will hopefully be making the transition soon. I'll update the README once that's complete or let me know what you find if you migrate your project first.
|
@@ -50,8 +43,6 @@ At the moment, a migration would be needed that:
|
|
50
43
|
- Moves each attribute record to a key => value pair for the simple_eav_column on the model using acts_as_eav_model
|
51
44
|
- Destroys each attribute record
|
52
45
|
|
53
|
-
You'd also need to review where your models using acts_as_eav_model are initialized. If they depended on the single incompatibility mentioned above those calls will need to be updated.
|
54
|
-
|
55
46
|
== Contributing
|
56
47
|
|
57
48
|
* Fork the project.
|
data/lib/simple_eav.rb
CHANGED
@@ -17,6 +17,10 @@ module SimpleEav
|
|
17
17
|
def simple_eav_column
|
18
18
|
self.class.simple_eav_column
|
19
19
|
end
|
20
|
+
|
21
|
+
def actual_columns_of_table
|
22
|
+
self.class.columns.map{|col| col.name.to_sym }
|
23
|
+
end
|
20
24
|
|
21
25
|
def simple_eav_attributes
|
22
26
|
self.send(simple_eav_column.to_sym) || {}
|
@@ -25,6 +29,20 @@ module SimpleEav
|
|
25
29
|
def simple_eav_attributes=(attributes={})
|
26
30
|
self.send("#{simple_eav_column}=", attributes)
|
27
31
|
end
|
32
|
+
|
33
|
+
def attributes=(_attributes={})
|
34
|
+
#Iterate over each attribute:
|
35
|
+
# - skip columns that are actually defined in the db
|
36
|
+
# - remove undefined columns to prevent UnknownAttribute::Error from being thrown
|
37
|
+
simple_eav_attrs = read_attribute(:simple_eav_column) || {}
|
38
|
+
_attributes.each do |column,value|
|
39
|
+
next if actual_columns_of_table.include?(column)
|
40
|
+
simple_eav_attrs[column] = value
|
41
|
+
_attributes.delete(column)
|
42
|
+
end
|
43
|
+
self.simple_eav_attributes = simple_eav_attrs
|
44
|
+
super(_attributes)
|
45
|
+
end
|
28
46
|
|
29
47
|
def respond_to?(method, include_private=false)
|
30
48
|
super || simple_eav_attributes.has_key?(method)
|
data/lib/version.rb
CHANGED
data/simple_eav.gemspec
CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
|
|
7
7
|
s.version = SimpleEav::VERSION
|
8
8
|
s.authors = ["Tim Linquist"]
|
9
9
|
s.email = ["tim.linquist@gmail.com"]
|
10
|
-
s.homepage =
|
10
|
+
s.homepage = %q{http://github.com/timo3377/simple_eav}
|
11
11
|
s.summary = %q{A simple alternative to acts_as_eav_model.}
|
12
12
|
s.description = %q{A simple alternative to acts_as_eav_model that works with ActiveRecord without any monkey patching needed. Acts_as_eav_model's gives a model the ability to have any number of custom attributes. This project has the same goal. The difference being maintaining utmost compatability with ActiveRecord::Base.}
|
13
13
|
|
data/spec/lib/simple_eav_spec.rb
CHANGED
@@ -13,17 +13,52 @@ describe SimpleEav do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
describe "Expected ActiveRecord behavior" do
|
16
|
-
|
16
|
+
describe "given a hash of undefined attributes" do
|
17
|
+
it "creates a person with a name" do
|
18
|
+
person = Person.create(:name=>'Joseph')
|
19
|
+
person.name.should eql 'Joseph'
|
20
|
+
end
|
21
|
+
it "saves the person's name" do
|
22
|
+
person = Person.new(:name=>'Jeremiah')
|
23
|
+
person.save!
|
24
|
+
person.reload
|
25
|
+
person.name.should eql 'Jeremiah'
|
26
|
+
end
|
27
|
+
it "updates the person's name" do
|
28
|
+
person = Person.create(:name=>'Joseph')
|
29
|
+
person.update_attributes(:name=>'Joe')
|
30
|
+
person.name.should eql 'Joe'
|
31
|
+
end
|
32
|
+
it "initializes the person with a name" do
|
33
|
+
person = Person.new(:name=>'John')
|
34
|
+
person.name.should eql 'John'
|
35
|
+
end
|
36
|
+
end
|
37
|
+
describe "given a hash of defined attributes" do
|
38
|
+
it "updates the person's age" do
|
39
|
+
person = Person.create(:age=>98)
|
40
|
+
person.update_attributes(:age=>99)
|
41
|
+
person.save!
|
42
|
+
person.reload
|
43
|
+
person.age.to_i.should eql 99
|
44
|
+
end
|
45
|
+
it "does not set the age in the simple eav attributes" do
|
46
|
+
person = Person.create(:age=>97, :new_age=>98)
|
47
|
+
person.simple_eav_attributes.should_not have_key :age
|
48
|
+
person.simple_eav_attributes.should have_key :new_age
|
49
|
+
end
|
50
|
+
end
|
51
|
+
it "sets all of the attributes" do
|
17
52
|
person = Person.create!({
|
18
|
-
:age=>
|
53
|
+
:age=>99,
|
19
54
|
:simple_attributes=>{
|
20
55
|
:name=>'John'
|
21
56
|
}
|
22
57
|
})
|
23
|
-
person.age.should eql(
|
58
|
+
person.age.should eql(99)
|
24
59
|
person.name.should eql('John')
|
25
60
|
end
|
26
|
-
it "
|
61
|
+
it "serializes and deserializes the simple_eav attributes" do
|
27
62
|
person = Person.new({:simple_attributes=>{
|
28
63
|
:name=>'John'
|
29
64
|
}})
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: simple_eav
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.0
|
5
|
+
version: 0.1.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-06-
|
13
|
+
date: 2011-06-16 00:00:00 -07:00
|
14
|
+
default_executable:
|
14
15
|
dependencies:
|
15
16
|
- !ruby/object:Gem::Dependency
|
16
17
|
name: activerecord
|
@@ -79,7 +80,8 @@ files:
|
|
79
80
|
- spec/lib/simple_eav_spec.rb
|
80
81
|
- spec/spec_helper.rb
|
81
82
|
- spec/support/person.rb
|
82
|
-
|
83
|
+
has_rdoc: true
|
84
|
+
homepage: http://github.com/timo3377/simple_eav
|
83
85
|
licenses: []
|
84
86
|
|
85
87
|
post_install_message:
|
@@ -102,7 +104,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
102
104
|
requirements: []
|
103
105
|
|
104
106
|
rubyforge_project:
|
105
|
-
rubygems_version: 1.
|
107
|
+
rubygems_version: 1.5.0
|
106
108
|
signing_key:
|
107
109
|
specification_version: 3
|
108
110
|
summary: A simple alternative to acts_as_eav_model.
|