has_custom_fields 0.0.5 → 0.1.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.
- data/Gemfile +7 -0
- data/README.md +114 -0
- data/Rakefile +16 -107
- data/has_custom_fields.gemspec +25 -40
- data/init.rb +4 -0
- data/lib/has_custom_fields.rb +45 -396
- data/lib/has_custom_fields/base.rb +20 -0
- data/lib/has_custom_fields/class_methods.rb +212 -0
- data/lib/has_custom_fields/instance_methods.rb +124 -0
- data/lib/has_custom_fields/railtie.rb +25 -0
- data/lib/has_custom_fields/version.rb +3 -0
- data/spec/db/database.yml +21 -0
- data/spec/db/schema.rb +43 -0
- data/spec/has_custom_fields_spec.rb +150 -0
- data/spec/spec_helper.rb +28 -25
- data/spec/test_models/organization.rb +3 -0
- data/spec/test_models/user.rb +6 -0
- metadata +66 -31
- data/README.rdoc +0 -117
- data/SPECDOC +0 -23
- data/VERSION +0 -1
- data/has_custom_fields.tmproj +0 -63
- data/lib/custom_fields/custom_field_base.rb +0 -29
- data/spec/database.yml +0 -12
- data/spec/debug.log +0 -3211
- data/spec/fixtures/document.rb +0 -7
- data/spec/fixtures/people.yml +0 -4
- data/spec/fixtures/person.rb +0 -13
- data/spec/fixtures/person_contact_infos.yml +0 -10
- data/spec/fixtures/post.rb +0 -6
- data/spec/fixtures/post_attributes.yml +0 -15
- data/spec/fixtures/posts.yml +0 -9
- data/spec/fixtures/preference.rb +0 -5
- data/spec/fixtures/preferences.yml +0 -10
- data/spec/models/eav_model_with_no_arguments_spec.rb +0 -82
- data/spec/models/eav_model_with_options_spec.rb +0 -38
- data/spec/models/eav_validation_spec.rb +0 -12
- data/spec/rcov.opts +0 -1
- data/spec/schema.rb +0 -50
- data/spec/spec.opts +0 -2
data/spec/fixtures/document.rb
DELETED
data/spec/fixtures/people.yml
DELETED
data/spec/fixtures/person.rb
DELETED
@@ -1,13 +0,0 @@
|
|
1
|
-
class Person < ActiveRecord::Base
|
2
|
-
|
3
|
-
has_custom_field_behavior :class_name => 'Preference',
|
4
|
-
:name_field => :key
|
5
|
-
|
6
|
-
has_custom_field_behavior :class_name => 'PersonContactInfo',
|
7
|
-
:foreign_key => :contact_id,
|
8
|
-
:fields => %w(phone aim icq)
|
9
|
-
|
10
|
-
def custom_field_attributes(model)
|
11
|
-
model == Preference ? %w(project_search project_order) : nil
|
12
|
-
end
|
13
|
-
end
|
data/spec/fixtures/post.rb
DELETED
@@ -1,15 +0,0 @@
|
|
1
|
-
post_one_comment:
|
2
|
-
id: 1
|
3
|
-
post_id: 1
|
4
|
-
name: comment
|
5
|
-
value: Foo Bar Industries gets two thumbs up
|
6
|
-
post_one_intro:
|
7
|
-
id: 2
|
8
|
-
post_id: 1
|
9
|
-
name: intro
|
10
|
-
value: We deliver quality foobars to consumers nationwide and around the globe
|
11
|
-
post_one_teaser:
|
12
|
-
id: 3
|
13
|
-
post_id: 1
|
14
|
-
name: teaser
|
15
|
-
value: Coming October 7, the foobarantator
|
data/spec/fixtures/posts.yml
DELETED
data/spec/fixtures/preference.rb
DELETED
@@ -1,82 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "ActiveRecord Model annotated with 'has_custom_field_behavior' with no options in declaration" do
|
4
|
-
fixtures :posts, :post_attributes
|
5
|
-
|
6
|
-
it "should have many attributes" do
|
7
|
-
post_attr = Post.find_by_title("Hello World").post_attributes
|
8
|
-
post_attr[0].should be_instance_of(PostAttribute)
|
9
|
-
post_attr.size.should == 3
|
10
|
-
end
|
11
|
-
|
12
|
-
it "should create new attribute on save" do
|
13
|
-
blog_post = Post.find_by_title("Following up from my first post.")
|
14
|
-
blog_post.send :write_attribute, 'new_attribute', 'new_value'
|
15
|
-
|
16
|
-
new_attribute = blog_post.post_attributes.to_a.find do |attribute|
|
17
|
-
attribute.name == 'new_attribute'
|
18
|
-
end
|
19
|
-
new_attribute.should_not be_nil
|
20
|
-
|
21
|
-
blog_post.send(:read_attribute, 'new_attribute').should == 'new_value'
|
22
|
-
PostAttribute.find_by_name_and_post_id('new_attribute', 2).should be_nil
|
23
|
-
blog_post.save!
|
24
|
-
|
25
|
-
PostAttribute.find_by_name_and_post_id('new_attribute', 2).value.should == 'new_value'
|
26
|
-
blog_post.send(:read_attribute, 'new_attribute').should == 'new_value'
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
it "should delete attribute" do
|
31
|
-
blog_post = Post.find_by_title("Hello World")
|
32
|
-
blog_post.send(:write_attribute, 'comment', nil)
|
33
|
-
|
34
|
-
comment = blog_post.post_attributes.find_by_name('comment')
|
35
|
-
comment.should_not be_nil
|
36
|
-
|
37
|
-
blog_post.send(:read_attribute, 'comment').should be_nil
|
38
|
-
blog_post.save!
|
39
|
-
|
40
|
-
blog_post.send(:read_attribute, 'comment').should be_nil
|
41
|
-
|
42
|
-
comment = blog_post.post_attributes.find_by_name('comment')
|
43
|
-
blog_post.send(:read_attribute, 'comment').should be_nil
|
44
|
-
PostAttribute.find_by_id(1).should be_nil
|
45
|
-
end
|
46
|
-
|
47
|
-
it "should write eav attributes to attributes table" do
|
48
|
-
blog_post = Post.find_by_title("Hello World")
|
49
|
-
blog_post.send(:write_attribute, 'intro', 'Blah Blah Blah')
|
50
|
-
blog_post.send(:read_attribute, 'intro').should == 'Blah Blah Blah'
|
51
|
-
PostAttribute.find(2).value.should_not == 'Blah Blah Blah'
|
52
|
-
blog_post.save!
|
53
|
-
PostAttribute.find(2).value.should == 'Blah Blah Blah'
|
54
|
-
end
|
55
|
-
|
56
|
-
it "should return nil when attribute does not exist" do
|
57
|
-
blog_post = Post.find_by_title("Hello World")
|
58
|
-
blog_post.send(:read_attribute, 'not_exist').should be_nil
|
59
|
-
end
|
60
|
-
|
61
|
-
it "should use method missing to make attribute seem as native property" do
|
62
|
-
blog_post = Post.find_by_title("Hello World")
|
63
|
-
blog_post.comment.should == 'Foo Bar Industries gets two thumbs up'
|
64
|
-
blog_post.intro.should == 'We deliver quality foobars to consumers nationwide and around the globe'
|
65
|
-
blog_post.teaser.should == 'Coming October 7, the foobarantator'
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should read attributes using subscript notation" do
|
69
|
-
blog_post = Post.find_by_title("Hello World")
|
70
|
-
blog_post['comment'].should == 'Foo Bar Industries gets two thumbs up'
|
71
|
-
blog_post['intro'].should == 'We deliver quality foobars to consumers nationwide and around the globe'
|
72
|
-
blog_post['teaser'].should == 'Coming October 7, the foobarantator'
|
73
|
-
end
|
74
|
-
|
75
|
-
it "should read the attribute when invoking 'read_attribute'" do
|
76
|
-
blog_post = Post.find_by_title("Hello World")
|
77
|
-
blog_post.send(:read_attribute, 'comment').should == 'Foo Bar Industries gets two thumbs up'
|
78
|
-
blog_post.send(:read_attribute, 'intro').should == 'We deliver quality foobars to consumers nationwide and around the globe'
|
79
|
-
blog_post.send(:read_attribute, 'teaser').should == 'Coming October 7, the foobarantator'
|
80
|
-
end
|
81
|
-
|
82
|
-
end
|
@@ -1,38 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "ActiveRecord Model annotated with 'has_custom_field_behavior' with options in declaration" do
|
4
|
-
fixtures :people, :preferences, :person_contact_infos
|
5
|
-
|
6
|
-
it "should be 'has_many' association on both sides" do
|
7
|
-
marcus = Person.find_by_name('Marcus Wyatt')
|
8
|
-
|
9
|
-
prefs = marcus.preferences
|
10
|
-
prefs[0].should be_instance_of(Preference)
|
11
|
-
prefs.size.should == 2
|
12
|
-
|
13
|
-
contact_info = marcus.person_contact_infos
|
14
|
-
contact_info[0].should be_instance_of(PersonContactInfo)
|
15
|
-
contact_info.size.should == 2
|
16
|
-
end
|
17
|
-
|
18
|
-
it "should only allow restricted fields when specified (:fields => %w(phone aim icq))" do
|
19
|
-
marcus = Person.find_by_name('Marcus Wyatt')
|
20
|
-
marcus.aim.should == 'marcus.wyatt'
|
21
|
-
lambda { marcus.doesnt_exist }.should raise_error(NoMethodError)
|
22
|
-
end
|
23
|
-
|
24
|
-
it "should raise 'NoMethodError' when attribute not in 'custom_field_attributes' method array" do
|
25
|
-
marcus = Person.find_by_name('Marcus Wyatt')
|
26
|
-
|
27
|
-
marcus.project_order.should == 'name'
|
28
|
-
lambda { marcus.project_blah }.should raise_error(NoMethodError)
|
29
|
-
end
|
30
|
-
|
31
|
-
it "should raise 'NoMethodError' when attribute does not satisfy 'is_custom_field_attribute?' method" do
|
32
|
-
doc = Document.new
|
33
|
-
|
34
|
-
doc.copyright_attr.should be_nil
|
35
|
-
lambda { doc.no_exist }.should raise_error(NoMethodError)
|
36
|
-
|
37
|
-
end
|
38
|
-
end
|
@@ -1,12 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
-
|
3
|
-
describe "Validations on ActiveRecord Model annotated with 'has_custom_field_behavior'" do
|
4
|
-
|
5
|
-
fixtures :posts, :post_attributes
|
6
|
-
|
7
|
-
it "should execute as normal (validates_presence_of)" do
|
8
|
-
post = Post.create :comment => 'No Intro', :teaser => 'This should fail'
|
9
|
-
post.errors[:intro].should == "can't be blank"
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
data/spec/rcov.opts
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
-x gems,spec
|
data/spec/schema.rb
DELETED
@@ -1,50 +0,0 @@
|
|
1
|
-
require File.join(File.dirname(__FILE__), 'fixtures/document')
|
2
|
-
ActiveRecord::Schema.define(:version => 0) do
|
3
|
-
|
4
|
-
create_table "people", :force => true do |t|
|
5
|
-
t.string "name"
|
6
|
-
t.string "email"
|
7
|
-
t.datetime "created_at"
|
8
|
-
t.datetime "updated_at"
|
9
|
-
end
|
10
|
-
|
11
|
-
create_table "person_contact_infos", :force => true do |t|
|
12
|
-
t.integer "contact_id", :null => false
|
13
|
-
t.string "name", :null => false
|
14
|
-
t.string "value", :null => false
|
15
|
-
end
|
16
|
-
|
17
|
-
create_table "posts", :force => true do |t|
|
18
|
-
t.string "title"
|
19
|
-
t.text "body"
|
20
|
-
t.datetime "created_at"
|
21
|
-
t.datetime "updated_at"
|
22
|
-
end
|
23
|
-
|
24
|
-
create_table "post_attributes", :force => true do |t|
|
25
|
-
t.integer "post_id", :null => false
|
26
|
-
t.string "name", :null => false
|
27
|
-
t.string "value", :null => false
|
28
|
-
t.datetime "created_at"
|
29
|
-
t.datetime "updated_at"
|
30
|
-
end
|
31
|
-
|
32
|
-
create_table "preferences", :force => true do |t|
|
33
|
-
t.integer "person_id", :null => false
|
34
|
-
t.string "key", :null => false
|
35
|
-
t.string "value", :null => false
|
36
|
-
end
|
37
|
-
|
38
|
-
create_table "documents", :force => true do |t|
|
39
|
-
t.string "name", :null => false
|
40
|
-
t.datetime "created_at"
|
41
|
-
t.datetime "updated_at"
|
42
|
-
end
|
43
|
-
|
44
|
-
create_table "document_attributes", :force => true do |t|
|
45
|
-
t.integer "document_id", :null => false
|
46
|
-
t.string "name", :null => false
|
47
|
-
t.string "value", :null => false
|
48
|
-
end
|
49
|
-
|
50
|
-
end
|
data/spec/spec.opts
DELETED