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.
@@ -1,7 +0,0 @@
1
- class Document < ActiveRecord::Base
2
- has_custom_field_behavior
3
-
4
- def is_custom_field_attribute?(attr_name, model)
5
- attr_name =~ /attr$/
6
- end
7
- end
@@ -1,4 +0,0 @@
1
- marcus:
2
- id: 1
3
- name: Marcus Wyatt
4
- email: marcus@example.com
@@ -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
@@ -1,10 +0,0 @@
1
- marcus_aim:
2
- id: 1
3
- contact_id: 1
4
- name: aim
5
- value: marcus.wyatt
6
- marcus_phone:
7
- id: 2
8
- contact_id: 1
9
- name: phone
10
- value: 021 356 0986
@@ -1,6 +0,0 @@
1
- class Post < ActiveRecord::Base
2
-
3
- has_custom_field_behavior
4
-
5
- validates_presence_of :intro, :message => "can't be blank", :on => :create
6
- end
@@ -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
@@ -1,9 +0,0 @@
1
- one:
2
- id: 1
3
- title: Hello World
4
- body: This is my first blog post. Great!
5
-
6
- two:
7
- id: 2
8
- title: Following up from my first post.
9
- body: I'm working on my first plugin today.
@@ -1,5 +0,0 @@
1
- class Preference < ActiveRecord::Base
2
- def to_s
3
- "#{key}: #{value}"
4
- end
5
- end
@@ -1,10 +0,0 @@
1
- marcus_project_search:
2
- id: 1
3
- person_id: 1
4
- key: project_search
5
- value: project.owner LIKE '%anderson%'
6
- marcus_project_order:
7
- id: 2
8
- person_id: 1
9
- key: project_order
10
- value: name
@@ -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
@@ -1 +0,0 @@
1
- -x gems,spec
@@ -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
@@ -1,2 +0,0 @@
1
- --diff
2
- --color