has_custom_fields 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
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
@@ -0,0 +1,4 @@
1
+ marcus:
2
+ id: 1
3
+ name: Marcus Wyatt
4
+ email: marcus@example.com
@@ -0,0 +1,13 @@
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
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,6 @@
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
@@ -0,0 +1,15 @@
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
@@ -0,0 +1,9 @@
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.
@@ -0,0 +1,5 @@
1
+ class Preference < ActiveRecord::Base
2
+ def to_s
3
+ "#{key}: #{value}"
4
+ end
5
+ end
@@ -0,0 +1,10 @@
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
@@ -0,0 +1,82 @@
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
@@ -0,0 +1,38 @@
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
@@ -0,0 +1,12 @@
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 ADDED
@@ -0,0 +1 @@
1
+ -x gems,spec
data/spec/schema.rb ADDED
@@ -0,0 +1,50 @@
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 ADDED
@@ -0,0 +1,2 @@
1
+ --diff
2
+ --color
@@ -0,0 +1,34 @@
1
+ $:.unshift(File.dirname(__FILE__) + '/../lib')
2
+
3
+ ENV["RAILS_ENV"] ||= "test"
4
+
5
+ require "rubygems"
6
+ require 'spec'
7
+ require File.expand_path(File.join(File.dirname(__FILE__), "../../../../config/environment"))
8
+ require 'spec/rails'
9
+ require 'active_record/fixtures'
10
+
11
+ begin
12
+ require 'ruby-debug'
13
+ Debugger.start
14
+ rescue LoadError
15
+ end
16
+
17
+ require "acts_as_custom_fields"
18
+
19
+ config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
20
+ ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
21
+ ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'mysql'])
22
+
23
+ plugin_fixture_path = File.expand_path(File.dirname(__FILE__) + "/fixtures/")
24
+ $LOAD_PATH.unshift(plugin_fixture_path)
25
+
26
+ Spec::Runner.configure do |config|
27
+ config.use_transactional_fixtures = true
28
+ config.use_instantiated_fixtures = false
29
+ config.fixture_path = plugin_fixture_path
30
+ end
31
+
32
+ load(File.dirname(__FILE__) + "/schema.rb")
33
+
34
+ alias :doing :lambda
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_custom_fields
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.0.1
6
+ platform: ruby
7
+ authors:
8
+ - kylejginavan
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-04 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: builder
17
+ prerelease: false
18
+ requirement: &id001 !ruby/object:Gem::Requirement
19
+ none: false
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ type: :runtime
25
+ version_requirements: *id001
26
+ description: Uses a vertical schema to add custom fields.
27
+ email: kylejginavan@gmail.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - LICENSE
34
+ - README.rdoc
35
+ files:
36
+ - LICENSE
37
+ - README.rdoc
38
+ - Rakefile
39
+ - SPECDOC
40
+ - VERSION
41
+ - has_custom_fields.tmproj
42
+ - lib/custom_fields/custom_field_base.rb
43
+ - lib/has_custom_fields.rb
44
+ - spec/database.yml
45
+ - spec/debug.log
46
+ - spec/fixtures/document.rb
47
+ - spec/fixtures/people.yml
48
+ - spec/fixtures/person.rb
49
+ - spec/fixtures/person_contact_infos.yml
50
+ - spec/fixtures/post.rb
51
+ - spec/fixtures/post_attributes.yml
52
+ - spec/fixtures/posts.yml
53
+ - spec/fixtures/preference.rb
54
+ - spec/fixtures/preferences.yml
55
+ - spec/models/eav_model_with_no_arguments_spec.rb
56
+ - spec/models/eav_model_with_options_spec.rb
57
+ - spec/models/eav_validation_spec.rb
58
+ - spec/rcov.opts
59
+ - spec/schema.rb
60
+ - spec/spec.opts
61
+ - spec/spec_helper.rb
62
+ homepage: http://github.com/kylejginavan/has_custom_fields
63
+ licenses: []
64
+
65
+ post_install_message:
66
+ rdoc_options: []
67
+
68
+ require_paths:
69
+ - lib
70
+ required_ruby_version: !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: "0"
76
+ required_rubygems_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: "0"
82
+ requirements: []
83
+
84
+ rubyforge_project:
85
+ rubygems_version: 1.8.8
86
+ signing_key:
87
+ specification_version: 3
88
+ summary: The easy way to add custom fields to any Rails model.
89
+ test_files:
90
+ - spec/fixtures/document.rb
91
+ - spec/fixtures/person.rb
92
+ - spec/fixtures/post.rb
93
+ - spec/fixtures/preference.rb
94
+ - spec/models/eav_model_with_no_arguments_spec.rb
95
+ - spec/models/eav_model_with_options_spec.rb
96
+ - spec/models/eav_validation_spec.rb
97
+ - spec/schema.rb
98
+ - spec/spec_helper.rb