acts_as_eav_model 0.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (80) hide show
  1. data/.rvmrc +1 -0
  2. data/CHANGELOG +3 -0
  3. data/Gemfile +13 -0
  4. data/Gemfile.lock +92 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +117 -0
  7. data/Rakefile +46 -0
  8. data/SPECDOC +23 -0
  9. data/TODO +0 -0
  10. data/VERSION +1 -0
  11. data/doc/classes/ActiveRecord/Acts/EavModel.html +213 -0
  12. data/doc/classes/ActiveRecord/Acts/EavModel/ClassMethods.html +343 -0
  13. data/doc/classes/ActiveRecord/Acts/EavModel/InstanceMethods.html +474 -0
  14. data/doc/classes/ActiveRecord/Acts/EavModel/InstanceMethods/ClassMethods.html +192 -0
  15. data/doc/classes/ActiveRecord/Base.html +170 -0
  16. data/doc/created.rid +1 -0
  17. data/doc/files/CHANGELOG.html +113 -0
  18. data/doc/files/MIT-LICENSE.html +129 -0
  19. data/doc/files/README_rdoc.html +248 -0
  20. data/doc/files/SPECDOC.html +170 -0
  21. data/doc/files/lib/acts_as_eav_model_rb.html +101 -0
  22. data/doc/fr_class_index.html +31 -0
  23. data/doc/fr_file_index.html +31 -0
  24. data/doc/fr_method_index.html +40 -0
  25. data/doc/index.html +24 -0
  26. data/doc/rdoc-style.css +208 -0
  27. data/init.rb +1 -0
  28. data/install.rb +1 -0
  29. data/lib/acts_as_eav_model.rb +542 -0
  30. data/spec/dummy/Rakefile +7 -0
  31. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  32. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  33. data/spec/dummy/app/models/document.rb +7 -0
  34. data/spec/dummy/app/models/person.rb +13 -0
  35. data/spec/dummy/app/models/person_contact_info.rb +2 -0
  36. data/spec/dummy/app/models/post.rb +4 -0
  37. data/spec/dummy/app/models/post_attribute.rb +2 -0
  38. data/spec/dummy/app/models/preference.rb +5 -0
  39. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  40. data/spec/dummy/config.ru +4 -0
  41. data/spec/dummy/config/application.rb +45 -0
  42. data/spec/dummy/config/boot.rb +10 -0
  43. data/spec/dummy/config/database.yml +22 -0
  44. data/spec/dummy/config/environment.rb +5 -0
  45. data/spec/dummy/config/environments/development.rb +26 -0
  46. data/spec/dummy/config/environments/production.rb +49 -0
  47. data/spec/dummy/config/environments/test.rb +35 -0
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  49. data/spec/dummy/config/initializers/inflections.rb +10 -0
  50. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  51. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  52. data/spec/dummy/config/initializers/session_store.rb +8 -0
  53. data/spec/dummy/config/locales/en.yml +5 -0
  54. data/spec/dummy/config/routes.rb +58 -0
  55. data/spec/dummy/db/development.sqlite3 +0 -0
  56. data/spec/dummy/db/schema.rb +51 -0
  57. data/spec/dummy/public/404.html +26 -0
  58. data/spec/dummy/public/422.html +26 -0
  59. data/spec/dummy/public/500.html +26 -0
  60. data/spec/dummy/public/favicon.ico +0 -0
  61. data/spec/dummy/public/javascripts/application.js +2 -0
  62. data/spec/dummy/public/javascripts/controls.js +965 -0
  63. data/spec/dummy/public/javascripts/dragdrop.js +974 -0
  64. data/spec/dummy/public/javascripts/effects.js +1123 -0
  65. data/spec/dummy/public/javascripts/prototype.js +6001 -0
  66. data/spec/dummy/public/javascripts/rails.js +175 -0
  67. data/spec/dummy/public/stylesheets/.gitkeep +0 -0
  68. data/spec/dummy/script/rails +6 -0
  69. data/spec/fixtures/people.yml +4 -0
  70. data/spec/fixtures/person_contact_infos.yml +10 -0
  71. data/spec/fixtures/post_attributes.yml +15 -0
  72. data/spec/fixtures/posts.yml +9 -0
  73. data/spec/fixtures/preferences.yml +10 -0
  74. data/spec/models/eav_model_with_no_arguments_spec.rb +82 -0
  75. data/spec/models/eav_model_with_options_spec.rb +37 -0
  76. data/spec/models/eav_validation_spec.rb +11 -0
  77. data/spec/schema.rb +50 -0
  78. data/spec/spec_helper.rb +38 -0
  79. data/uninstall.rb +1 -0
  80. metadata +213 -0
@@ -0,0 +1,175 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+ })();
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,4 @@
1
+ marcus:
2
+ id: 1
3
+ name: Marcus Wyatt
4
+ email: marcus@example.com
@@ -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,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,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 'spec_helper'
2
+
3
+ describe "ActiveRecord Model annotated with 'has_eav_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,37 @@
1
+ require 'spec_helper'
2
+ describe "ActiveRecord Model annotated with 'has_eav_behavior' with options in declaration" do
3
+ fixtures :people, :preferences, :person_contact_infos
4
+
5
+ it "should be 'has_many' association on both sides" do
6
+ marcus = Person.find_by_name('Marcus Wyatt')
7
+
8
+ prefs = marcus.preferences
9
+ prefs[0].should be_instance_of(Preference)
10
+ prefs.size.should == 2
11
+
12
+ contact_info = marcus.person_contact_infos
13
+ contact_info[0].should be_instance_of(PersonContactInfo)
14
+ contact_info.size.should == 2
15
+ end
16
+
17
+ it "should only allow restricted fields when specified (:fields => %w(phone aim icq))" do
18
+ marcus = Person.find_by_name('Marcus Wyatt')
19
+ marcus.aim.should == 'marcus.wyatt'
20
+ lambda { marcus.doesnt_exist }.should raise_error(NoMethodError)
21
+ end
22
+
23
+ it "should raise 'NoMethodError' when attribute not in 'eav_attributes' method array" do
24
+ marcus = Person.find_by_name('Marcus Wyatt')
25
+
26
+ marcus.project_order.should == 'name'
27
+ lambda { marcus.project_blah }.should raise_error(NoMethodError)
28
+ end
29
+
30
+ it "should raise 'NoMethodError' when attribute does not satisfy 'is_eav_attribute?' method" do
31
+ doc = Document.new
32
+
33
+ doc.copyright_attr.should be_nil
34
+ lambda { doc.no_exist }.should raise_error(NoMethodError)
35
+
36
+ end
37
+ end
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+ describe "Validations on ActiveRecord Model annotated with 'has_eav_behavior'" do
3
+
4
+ fixtures :posts, :post_attributes
5
+
6
+ it "should execute as normal (validates_presence_of)" do
7
+ post = Post.create :comment => 'No Intro', :teaser => 'This should fail'
8
+ post.errors[:intro].should == ["can't be blank"]
9
+ end
10
+
11
+ end
@@ -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
@@ -0,0 +1,38 @@
1
+
2
+ # Configure Rails Envinronment
3
+ ENV["RAILS_ENV"] = "test"
4
+
5
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
6
+ require "rails/test_help"
7
+ require "rspec/rails"
8
+
9
+ #ActionMailer::Base.delivery_method = :test
10
+ ActionMailer::Base.perform_deliveries = false
11
+ #ActionMailer::Base.default_url_options[:host] = "test.com"
12
+
13
+ Rails.backtrace_cleaner.remove_silencers!
14
+
15
+ # Configure capybara for integration testing
16
+ #require "capybara/rails"
17
+ #Capybara.default_driver = :rack_test
18
+ #Capybara.default_selector = :css
19
+
20
+ # Run any available migration
21
+ ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
22
+
23
+ # Load support files
24
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
25
+ plugin_fixture_path = File.expand_path(File.dirname(__FILE__) + "/fixtures/")
26
+ $LOAD_PATH.unshift(plugin_fixture_path)
27
+
28
+ RSpec.configure do |config|
29
+ # Remove this line if you don't want Rspec's should and should_not
30
+ # methods or matchers
31
+ require 'rspec/expectations'
32
+ config.include Rspec::Matchers
33
+
34
+ # == Mock Framework
35
+ config.mock_with :rspec
36
+ config.fixture_path=plugin_fixture_path
37
+ end
38
+ load(File.dirname(__FILE__) + "/dummy/db/schema.rb")