acts_as_eav_model 0.0.0
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/.rvmrc +1 -0
- data/CHANGELOG +3 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +92 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +117 -0
- data/Rakefile +46 -0
- data/SPECDOC +23 -0
- data/TODO +0 -0
- data/VERSION +1 -0
- data/doc/classes/ActiveRecord/Acts/EavModel.html +213 -0
- data/doc/classes/ActiveRecord/Acts/EavModel/ClassMethods.html +343 -0
- data/doc/classes/ActiveRecord/Acts/EavModel/InstanceMethods.html +474 -0
- data/doc/classes/ActiveRecord/Acts/EavModel/InstanceMethods/ClassMethods.html +192 -0
- data/doc/classes/ActiveRecord/Base.html +170 -0
- data/doc/created.rid +1 -0
- data/doc/files/CHANGELOG.html +113 -0
- data/doc/files/MIT-LICENSE.html +129 -0
- data/doc/files/README_rdoc.html +248 -0
- data/doc/files/SPECDOC.html +170 -0
- data/doc/files/lib/acts_as_eav_model_rb.html +101 -0
- data/doc/fr_class_index.html +31 -0
- data/doc/fr_file_index.html +31 -0
- data/doc/fr_method_index.html +40 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/init.rb +1 -0
- data/install.rb +1 -0
- data/lib/acts_as_eav_model.rb +542 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/document.rb +7 -0
- data/spec/dummy/app/models/person.rb +13 -0
- data/spec/dummy/app/models/person_contact_info.rb +2 -0
- data/spec/dummy/app/models/post.rb +4 -0
- data/spec/dummy/app/models/post_attribute.rb +2 -0
- data/spec/dummy/app/models/preference.rb +5 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/database.yml +22 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/db/development.sqlite3 +0 -0
- data/spec/dummy/db/schema.rb +51 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +175 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/fixtures/people.yml +4 -0
- data/spec/fixtures/person_contact_infos.yml +10 -0
- data/spec/fixtures/post_attributes.yml +15 -0
- data/spec/fixtures/posts.yml +9 -0
- data/spec/fixtures/preferences.yml +10 -0
- data/spec/models/eav_model_with_no_arguments_spec.rb +82 -0
- data/spec/models/eav_model_with_options_spec.rb +37 -0
- data/spec/models/eav_validation_spec.rb +11 -0
- data/spec/schema.rb +50 -0
- data/spec/spec_helper.rb +38 -0
- data/uninstall.rb +1 -0
- metadata +213 -0
@@ -0,0 +1,343 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: ActiveRecord::Acts::EavModel::ClassMethods</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">ActiveRecord::Acts::EavModel::ClassMethods</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../../../files/lib/acts_as_eav_model_rb.html">
|
59
|
+
lib/acts_as_eav_model.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
</table>
|
66
|
+
</div>
|
67
|
+
<!-- banner header -->
|
68
|
+
|
69
|
+
<div id="bodyContent">
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
<div id="contextContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<div id="method-list">
|
80
|
+
<h3 class="section-bar">Methods</h3>
|
81
|
+
|
82
|
+
<div class="name-list">
|
83
|
+
<a href="#M000001">has_eav_behavior</a>
|
84
|
+
</div>
|
85
|
+
</div>
|
86
|
+
|
87
|
+
</div>
|
88
|
+
|
89
|
+
|
90
|
+
<!-- if includes -->
|
91
|
+
|
92
|
+
<div id="section">
|
93
|
+
|
94
|
+
|
95
|
+
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
|
100
|
+
|
101
|
+
<!-- if method_list -->
|
102
|
+
<div id="methods">
|
103
|
+
<h3 class="section-bar">Public Instance methods</h3>
|
104
|
+
|
105
|
+
<div id="method-M000001" class="method-detail">
|
106
|
+
<a name="M000001"></a>
|
107
|
+
|
108
|
+
<div class="method-heading">
|
109
|
+
<a href="#M000001" class="method-signature">
|
110
|
+
<span class="method-name">has_eav_behavior</span><span class="method-args">(options = {})</span>
|
111
|
+
</a>
|
112
|
+
</div>
|
113
|
+
|
114
|
+
<div class="method-description">
|
115
|
+
<p>
|
116
|
+
Will make the current class have eav behaviour.
|
117
|
+
</p>
|
118
|
+
<pre>
|
119
|
+
class Post < ActiveRecord::Base
|
120
|
+
has_eav_behavior
|
121
|
+
end
|
122
|
+
post = Post.find_by_title 'hello world'
|
123
|
+
puts "My post intro is: #{post.intro}"
|
124
|
+
post.teaser = 'An awesome introduction to the blog'
|
125
|
+
post.save
|
126
|
+
</pre>
|
127
|
+
<p>
|
128
|
+
The above example should work even though "intro" and
|
129
|
+
"teaser" are not attributes on the Post model.
|
130
|
+
</p>
|
131
|
+
<p>
|
132
|
+
The following options are available on for <a
|
133
|
+
href="ClassMethods.html#M000001">has_eav_behavior</a> to modify the
|
134
|
+
behavior. Reasonable defaults are provided:
|
135
|
+
</p>
|
136
|
+
<ul>
|
137
|
+
<li><tt>class_name</tt>: The class for the related model. This defaults to the
|
138
|
+
model name prepended to "Attribute". So for a "User"
|
139
|
+
model the class name would be "UserAttribute". The class can
|
140
|
+
actually exist (in that case the model file will be loaded through Rails
|
141
|
+
dependency system) or if it does not exist a basic model will be
|
142
|
+
dynamically defined for you. This allows you to implement custom methods on
|
143
|
+
the related class by simply defining the class manually.
|
144
|
+
|
145
|
+
</li>
|
146
|
+
<li><tt>table_name</tt>: The table for the related model. This defaults to the
|
147
|
+
attribute model‘s table name.
|
148
|
+
|
149
|
+
</li>
|
150
|
+
<li><tt>relationship_name</tt>: This is the name of the actual has_many
|
151
|
+
relationship. Most of the type this relationship will only be used
|
152
|
+
indirectly but it is there if the user wants more raw access. This defaults
|
153
|
+
to the class name underscored then pluralized finally turned into a symbol.
|
154
|
+
|
155
|
+
</li>
|
156
|
+
<li><tt>foreign_key</tt>: The key in the attribute table to relate back to the
|
157
|
+
model. This defaults to the model name underscored prepended to
|
158
|
+
"_id"
|
159
|
+
|
160
|
+
</li>
|
161
|
+
<li><tt>name_field</tt>: The field which stores the name of the attribute in
|
162
|
+
the related object
|
163
|
+
|
164
|
+
</li>
|
165
|
+
<li><tt>value_field</tt>: The field that stores the value in the related object
|
166
|
+
|
167
|
+
</li>
|
168
|
+
<li><tt>fields</tt>: A list of fields that are valid eav attributes. By default
|
169
|
+
this is "nil" which means that all field are valid. Use this
|
170
|
+
option if you want some fields to go to one flex attribute model while
|
171
|
+
other fields will go to another. As an alternative you can override the
|
172
|
+
eav_attributes method which will return a list of all valid flex
|
173
|
+
attributes. This is useful if you want to read the list of attributes from
|
174
|
+
another source to keep your code DRY. This method is given a single
|
175
|
+
argument which is the class for the related model. The following provide an
|
176
|
+
example:
|
177
|
+
|
178
|
+
</li>
|
179
|
+
</ul>
|
180
|
+
<pre>
|
181
|
+
class User < ActiveRecord::Base
|
182
|
+
has_eav_behavior :class_name => 'UserContactInfo'
|
183
|
+
has_eav_behavior :class_name => 'Preferences'
|
184
|
+
|
185
|
+
def eav_attributes(model)
|
186
|
+
case model
|
187
|
+
when UserContactInfo
|
188
|
+
%w(email phone aim yahoo msn)
|
189
|
+
when Preference
|
190
|
+
%w(project_search project_order user_search user_order)
|
191
|
+
else Array.new
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
|
196
|
+
marcus = User.find_by_login 'marcus'
|
197
|
+
marcus.email = 'marcus@example.com' # Will save to UserContactInfo model
|
198
|
+
marcus.project_order = 'name' # Will save to Preference
|
199
|
+
marcus.save # Carries out save so now values are in database
|
200
|
+
</pre>
|
201
|
+
<p>
|
202
|
+
Note the else clause in our case statement. Since an empty array is
|
203
|
+
returned for all other models (perhaps added later) then we can be certain
|
204
|
+
that only the above eav attributes are allowed.
|
205
|
+
</p>
|
206
|
+
<p>
|
207
|
+
If both a :fields option and eav_attributes method is defined the
|
208
|
+
<tt>fields</tt> option take precidence. This allows you to easily define
|
209
|
+
the field list inline for one model while implementing eav_attributes for
|
210
|
+
another model and not having eav_attributes need to determine what model it
|
211
|
+
is answering for. In both cases the list of flex attributes can be a list
|
212
|
+
of string or symbols
|
213
|
+
</p>
|
214
|
+
<p>
|
215
|
+
A final alternative to :fields and eav_attributes is the is_eav_attribute?
|
216
|
+
method. This method is given two arguments. The first is the attribute
|
217
|
+
being retrieved/saved the second is the Model we are testing for. If you
|
218
|
+
override this method then the eav_attributes method or the :fields option
|
219
|
+
will have no affect. Use of this method is ideal when you want to retrict
|
220
|
+
the attributes but do so in a algorithmic way. The following is an example:
|
221
|
+
</p>
|
222
|
+
<pre>
|
223
|
+
class User < ActiveRecord::Base
|
224
|
+
has_eav_behavior :class_name => 'UserContactInfo'
|
225
|
+
has_eav_behavior :class_name => 'Preferences'
|
226
|
+
|
227
|
+
def is_eav_attribute?(attr, model)
|
228
|
+
case attr.to_s
|
229
|
+
when /^contact_/ then true
|
230
|
+
when /^preference_/ then true
|
231
|
+
else
|
232
|
+
false
|
233
|
+
end
|
234
|
+
end
|
235
|
+
end
|
236
|
+
|
237
|
+
marcus = User.find_by_login 'marcus'
|
238
|
+
marcus.contact_phone = '021 654 9876'
|
239
|
+
marcus.contact_email = 'marcus@example.com'
|
240
|
+
marcus.preference_project_order = 'name'
|
241
|
+
marcus.some_attribute = 'blah' # If some_attribute is not defined on
|
242
|
+
# the model then method not found is thrown
|
243
|
+
</pre>
|
244
|
+
<p><a class="source-toggle" href="#"
|
245
|
+
onclick="toggleCode('M000001-source');return false;">[Source]</a></p>
|
246
|
+
<div class="method-source-code" id="M000001-source">
|
247
|
+
<pre>
|
248
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 200</span>
|
249
|
+
200: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">has_eav_behavior</span>(<span class="ruby-identifier">options</span> = {})
|
250
|
+
201:
|
251
|
+
202: <span class="ruby-comment cmt"># Provide default options</span>
|
252
|
+
203: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>] <span class="ruby-operator">||=</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class_name</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'Attribute'</span>
|
253
|
+
204: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:table_name</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>].<span class="ruby-identifier">tableize</span>
|
254
|
+
205: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:relationship_name</span>] <span class="ruby-operator">||=</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>].<span class="ruby-identifier">tableize</span>.<span class="ruby-identifier">to_sym</span>
|
255
|
+
206: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:foreign_key</span>] <span class="ruby-operator">||=</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class_name</span>.<span class="ruby-identifier">foreign_key</span>
|
256
|
+
207: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:base_foreign_key</span>] <span class="ruby-operator">||=</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">underscore</span>.<span class="ruby-identifier">foreign_key</span>
|
257
|
+
208: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:name_field</span>] <span class="ruby-operator">||=</span> <span class="ruby-value str">'name'</span>
|
258
|
+
209: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:value_field</span>] <span class="ruby-operator">||=</span> <span class="ruby-value str">'value'</span>
|
259
|
+
210: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:fields</span>].<span class="ruby-identifier">collect!</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">f</span><span class="ruby-operator">|</span> <span class="ruby-identifier">f</span>.<span class="ruby-identifier">to_s</span>} <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:fields</span>].<span class="ruby-identifier">nil?</span>
|
260
|
+
211: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:parent</span>] = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class_name</span>
|
261
|
+
212:
|
262
|
+
213: <span class="ruby-comment cmt"># Init option storage if necessary</span>
|
263
|
+
214: <span class="ruby-identifier">cattr_accessor</span> <span class="ruby-identifier">:eav_options</span>
|
264
|
+
215: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">eav_options</span> <span class="ruby-operator">||=</span> <span class="ruby-constant">Hash</span>.<span class="ruby-identifier">new</span>
|
265
|
+
216:
|
266
|
+
217: <span class="ruby-comment cmt"># Return if already processed.</span>
|
267
|
+
218: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">eav_options</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">include?</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>]
|
268
|
+
219:
|
269
|
+
220: <span class="ruby-comment cmt"># Attempt to load related class. If not create it</span>
|
270
|
+
221: <span class="ruby-keyword kw">begin</span>
|
271
|
+
222: <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>].<span class="ruby-identifier">constantize</span>
|
272
|
+
223: <span class="ruby-keyword kw">rescue</span>
|
273
|
+
224: <span class="ruby-constant">Object</span>.<span class="ruby-identifier">const_set</span>(<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>],
|
274
|
+
225: <span class="ruby-constant">Class</span>.<span class="ruby-identifier">new</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Base</span>)).<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword kw">do</span>
|
275
|
+
226: <span class="ruby-keyword kw">def</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">reloadable?</span> <span class="ruby-comment cmt">#:nodoc:</span>
|
276
|
+
227: <span class="ruby-keyword kw">false</span>
|
277
|
+
228: <span class="ruby-keyword kw">end</span>
|
278
|
+
229: <span class="ruby-keyword kw">end</span>
|
279
|
+
230: <span class="ruby-keyword kw">end</span>
|
280
|
+
231:
|
281
|
+
232: <span class="ruby-comment cmt"># Store options</span>
|
282
|
+
233: <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>]] = <span class="ruby-identifier">options</span>
|
283
|
+
234:
|
284
|
+
235: <span class="ruby-comment cmt"># Only mix instance methods once</span>
|
285
|
+
236: <span class="ruby-keyword kw">unless</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">included_modules</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Acts</span><span class="ruby-operator">::</span><span class="ruby-constant">EavModel</span><span class="ruby-operator">::</span><span class="ruby-constant">InstanceMethods</span>)
|
286
|
+
237: <span class="ruby-identifier">send</span> <span class="ruby-identifier">:include</span>, <span class="ruby-constant">ActiveRecord</span><span class="ruby-operator">::</span><span class="ruby-constant">Acts</span><span class="ruby-operator">::</span><span class="ruby-constant">EavModel</span><span class="ruby-operator">::</span><span class="ruby-constant">InstanceMethods</span>
|
287
|
+
238: <span class="ruby-keyword kw">end</span>
|
288
|
+
239:
|
289
|
+
240: <span class="ruby-comment cmt"># Modify attribute class</span>
|
290
|
+
241: <span class="ruby-identifier">attribute_class</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>].<span class="ruby-identifier">constantize</span>
|
291
|
+
242: <span class="ruby-identifier">base_class</span> = <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">name</span>.<span class="ruby-identifier">underscore</span>.<span class="ruby-identifier">to_sym</span>
|
292
|
+
243:
|
293
|
+
244: <span class="ruby-identifier">attribute_class</span>.<span class="ruby-identifier">class_eval</span> <span class="ruby-keyword kw">do</span>
|
294
|
+
245: <span class="ruby-identifier">belongs_to</span> <span class="ruby-identifier">base_class</span>, <span class="ruby-identifier">:foreign_key</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:base_foreign_key</span>]
|
295
|
+
246: <span class="ruby-identifier">alias_method</span> <span class="ruby-identifier">:base</span>, <span class="ruby-identifier">base_class</span> <span class="ruby-comment cmt"># For generic access</span>
|
296
|
+
247: <span class="ruby-keyword kw">end</span>
|
297
|
+
248:
|
298
|
+
249: <span class="ruby-comment cmt"># Modify main class</span>
|
299
|
+
250: <span class="ruby-identifier">class_eval</span> <span class="ruby-keyword kw">do</span>
|
300
|
+
251: <span class="ruby-identifier">has_many</span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:relationship_name</span>],
|
301
|
+
252: <span class="ruby-identifier">:class_name</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class_name</span>],
|
302
|
+
253: <span class="ruby-identifier">:table_name</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:table_name</span>],
|
303
|
+
254: <span class="ruby-identifier">:foreign_key</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:foreign_key</span>],
|
304
|
+
255: <span class="ruby-identifier">:dependent</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">:destroy</span>
|
305
|
+
256:
|
306
|
+
257: <span class="ruby-comment cmt"># The following is only setup once</span>
|
307
|
+
258: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">method_defined?</span> <span class="ruby-identifier">:method_missing_without_eav_behavior</span>
|
308
|
+
259:
|
309
|
+
260: <span class="ruby-comment cmt"># Carry out delayed actions before save</span>
|
310
|
+
261: <span class="ruby-identifier">after_validation_on_update</span> <span class="ruby-identifier">:save_modified_eav_attributes</span>
|
311
|
+
262:
|
312
|
+
263: <span class="ruby-comment cmt"># Make attributes seem real</span>
|
313
|
+
264: <span class="ruby-identifier">alias_method_chain</span> <span class="ruby-identifier">:method_missing</span>, <span class="ruby-identifier">:eav_behavior</span>
|
314
|
+
265:
|
315
|
+
266: <span class="ruby-identifier">private</span>
|
316
|
+
267:
|
317
|
+
268: <span class="ruby-identifier">alias_method_chain</span> <span class="ruby-identifier">:read_attribute</span>, <span class="ruby-identifier">:eav_behavior</span>
|
318
|
+
269: <span class="ruby-identifier">alias_method_chain</span> <span class="ruby-identifier">:write_attribute</span>, <span class="ruby-identifier">:eav_behavior</span>
|
319
|
+
270:
|
320
|
+
271: <span class="ruby-keyword kw">end</span>
|
321
|
+
272: <span class="ruby-keyword kw">end</span>
|
322
|
+
273:
|
323
|
+
274: <span class="ruby-identifier">create_attribute_table</span>
|
324
|
+
275:
|
325
|
+
276: <span class="ruby-keyword kw">end</span>
|
326
|
+
</pre>
|
327
|
+
</div>
|
328
|
+
</div>
|
329
|
+
</div>
|
330
|
+
|
331
|
+
|
332
|
+
</div>
|
333
|
+
|
334
|
+
|
335
|
+
</div>
|
336
|
+
|
337
|
+
|
338
|
+
<div id="validator-badges">
|
339
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
340
|
+
</div>
|
341
|
+
|
342
|
+
</body>
|
343
|
+
</html>
|
@@ -0,0 +1,474 @@
|
|
1
|
+
<?xml version="1.0" encoding="iso-8859-1"?>
|
2
|
+
<!DOCTYPE html
|
3
|
+
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
|
4
|
+
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
5
|
+
|
6
|
+
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
|
7
|
+
<head>
|
8
|
+
<title>Module: ActiveRecord::Acts::EavModel::InstanceMethods</title>
|
9
|
+
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
|
10
|
+
<meta http-equiv="Content-Script-Type" content="text/javascript" />
|
11
|
+
<link rel="stylesheet" href="../../../.././rdoc-style.css" type="text/css" media="screen" />
|
12
|
+
<script type="text/javascript">
|
13
|
+
// <![CDATA[
|
14
|
+
|
15
|
+
function popupCode( url ) {
|
16
|
+
window.open(url, "Code", "resizable=yes,scrollbars=yes,toolbar=no,status=no,height=150,width=400")
|
17
|
+
}
|
18
|
+
|
19
|
+
function toggleCode( id ) {
|
20
|
+
if ( document.getElementById )
|
21
|
+
elem = document.getElementById( id );
|
22
|
+
else if ( document.all )
|
23
|
+
elem = eval( "document.all." + id );
|
24
|
+
else
|
25
|
+
return false;
|
26
|
+
|
27
|
+
elemStyle = elem.style;
|
28
|
+
|
29
|
+
if ( elemStyle.display != "block" ) {
|
30
|
+
elemStyle.display = "block"
|
31
|
+
} else {
|
32
|
+
elemStyle.display = "none"
|
33
|
+
}
|
34
|
+
|
35
|
+
return true;
|
36
|
+
}
|
37
|
+
|
38
|
+
// Make codeblocks hidden by default
|
39
|
+
document.writeln( "<style type=\"text/css\">div.method-source-code { display: none }</style>" )
|
40
|
+
|
41
|
+
// ]]>
|
42
|
+
</script>
|
43
|
+
|
44
|
+
</head>
|
45
|
+
<body>
|
46
|
+
|
47
|
+
|
48
|
+
|
49
|
+
<div id="classHeader">
|
50
|
+
<table class="header-table">
|
51
|
+
<tr class="top-aligned-row">
|
52
|
+
<td><strong>Module</strong></td>
|
53
|
+
<td class="class-name-in-header">ActiveRecord::Acts::EavModel::InstanceMethods</td>
|
54
|
+
</tr>
|
55
|
+
<tr class="top-aligned-row">
|
56
|
+
<td><strong>In:</strong></td>
|
57
|
+
<td>
|
58
|
+
<a href="../../../../files/lib/acts_as_eav_model_rb.html">
|
59
|
+
lib/acts_as_eav_model.rb
|
60
|
+
</a>
|
61
|
+
<br />
|
62
|
+
</td>
|
63
|
+
</tr>
|
64
|
+
|
65
|
+
</table>
|
66
|
+
</div>
|
67
|
+
<!-- banner header -->
|
68
|
+
|
69
|
+
<div id="bodyContent">
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
<div id="contextContent">
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
</div>
|
78
|
+
|
79
|
+
<div id="method-list">
|
80
|
+
<h3 class="section-bar">Methods</h3>
|
81
|
+
|
82
|
+
<div class="name-list">
|
83
|
+
<a href="#M000011">each_eav_relation</a>
|
84
|
+
<a href="#M000003">eav_attributes</a>
|
85
|
+
<a href="#M000009">eav_related</a>
|
86
|
+
<a href="#M000010">exec_if_related</a>
|
87
|
+
<a href="#M000008">find_related_eav_attribute</a>
|
88
|
+
<a href="#M000002">is_eav_attribute?</a>
|
89
|
+
<a href="#M000007">method_missing_with_eav_behavior</a>
|
90
|
+
<a href="#M000005">read_attribute_with_eav_behavior</a>
|
91
|
+
<a href="#M000004">save_modified_eav_attributes</a>
|
92
|
+
<a href="#M000006">write_attribute_with_eav_behavior</a>
|
93
|
+
</div>
|
94
|
+
</div>
|
95
|
+
|
96
|
+
</div>
|
97
|
+
|
98
|
+
|
99
|
+
<!-- if includes -->
|
100
|
+
|
101
|
+
<div id="section">
|
102
|
+
|
103
|
+
<div id="class-list">
|
104
|
+
<h3 class="section-bar">Classes and Modules</h3>
|
105
|
+
|
106
|
+
Module <a href="InstanceMethods/ClassMethods.html" class="link">ActiveRecord::Acts::EavModel::InstanceMethods::ClassMethods</a><br />
|
107
|
+
|
108
|
+
</div>
|
109
|
+
|
110
|
+
|
111
|
+
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
|
116
|
+
<!-- if method_list -->
|
117
|
+
<div id="methods">
|
118
|
+
<h3 class="section-bar">Public Instance methods</h3>
|
119
|
+
|
120
|
+
<div id="method-M000003" class="method-detail">
|
121
|
+
<a name="M000003"></a>
|
122
|
+
|
123
|
+
<div class="method-heading">
|
124
|
+
<a href="#M000003" class="method-signature">
|
125
|
+
<span class="method-name">eav_attributes</span><span class="method-args">(model)</span>
|
126
|
+
</a>
|
127
|
+
</div>
|
128
|
+
|
129
|
+
<div class="method-description">
|
130
|
+
<p>
|
131
|
+
Return a list of valid eav attributes for the given model. Return nil if
|
132
|
+
any field is allowed. If you want to say no field is allowed then return an
|
133
|
+
empty array. If you just have a static list the :fields option is most
|
134
|
+
likely easier.
|
135
|
+
</p>
|
136
|
+
<p><a class="source-toggle" href="#"
|
137
|
+
onclick="toggleCode('M000003-source');return false;">[Source]</a></p>
|
138
|
+
<div class="method-source-code" id="M000003-source">
|
139
|
+
<pre>
|
140
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 347</span>
|
141
|
+
347: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">eav_attributes</span>(<span class="ruby-identifier">model</span>); <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">end</span>
|
142
|
+
</pre>
|
143
|
+
</div>
|
144
|
+
</div>
|
145
|
+
</div>
|
146
|
+
|
147
|
+
<div id="method-M000002" class="method-detail">
|
148
|
+
<a name="M000002"></a>
|
149
|
+
|
150
|
+
<div class="method-heading">
|
151
|
+
<a href="#M000002" class="method-signature">
|
152
|
+
<span class="method-name">is_eav_attribute?</span><span class="method-args">(attribute_name, model)</span>
|
153
|
+
</a>
|
154
|
+
</div>
|
155
|
+
|
156
|
+
<div class="method-description">
|
157
|
+
<p>
|
158
|
+
Will determine if the given attribute is a eav attribute on the given
|
159
|
+
model. Override this in your class to provide custom logic if the <a
|
160
|
+
href="InstanceMethods.html#M000003">eav_attributes</a> method or the
|
161
|
+
:fields option are not flexible enough. If you override this method :fields
|
162
|
+
and <a href="InstanceMethods.html#M000003">eav_attributes</a> will not
|
163
|
+
apply at all unless you implement them yourself.
|
164
|
+
</p>
|
165
|
+
<p><a class="source-toggle" href="#"
|
166
|
+
onclick="toggleCode('M000002-source');return false;">[Source]</a></p>
|
167
|
+
<div class="method-source-code" id="M000002-source">
|
168
|
+
<pre>
|
169
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 332</span>
|
170
|
+
332: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">is_eav_attribute?</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">model</span>)
|
171
|
+
333: <span class="ruby-identifier">attribute_name</span> = <span class="ruby-identifier">attribute_name</span>.<span class="ruby-identifier">to_s</span>
|
172
|
+
334: <span class="ruby-identifier">model_options</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>]
|
173
|
+
335: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">model_options</span>[<span class="ruby-identifier">:fields</span>].<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">attribute_name</span>) <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">model_options</span>[<span class="ruby-identifier">:fields</span>].<span class="ruby-identifier">nil?</span>
|
174
|
+
336: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">eav_attributes</span>(<span class="ruby-identifier">model</span>).<span class="ruby-identifier">collect</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">field</span><span class="ruby-operator">|</span> <span class="ruby-identifier">field</span>.<span class="ruby-identifier">to_s</span>}.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">attribute_name</span>) <span class="ruby-keyword kw">unless</span>
|
175
|
+
337: <span class="ruby-identifier">eav_attributes</span>(<span class="ruby-identifier">model</span>).<span class="ruby-identifier">nil?</span>
|
176
|
+
338: <span class="ruby-keyword kw">true</span>
|
177
|
+
339: <span class="ruby-keyword kw">end</span>
|
178
|
+
</pre>
|
179
|
+
</div>
|
180
|
+
</div>
|
181
|
+
</div>
|
182
|
+
|
183
|
+
<h3 class="section-bar">Private Instance methods</h3>
|
184
|
+
|
185
|
+
<div id="method-M000011" class="method-detail">
|
186
|
+
<a name="M000011"></a>
|
187
|
+
|
188
|
+
<div class="method-heading">
|
189
|
+
<a href="#M000011" class="method-signature">
|
190
|
+
<span class="method-name">each_eav_relation</span><span class="method-args">() {|kls.constantize| ...}</span>
|
191
|
+
</a>
|
192
|
+
</div>
|
193
|
+
|
194
|
+
<div class="method-description">
|
195
|
+
<p>
|
196
|
+
yields for each eav relation.
|
197
|
+
</p>
|
198
|
+
<p><a class="source-toggle" href="#"
|
199
|
+
onclick="toggleCode('M000011-source');return false;">[Source]</a></p>
|
200
|
+
<div class="method-source-code" id="M000011-source">
|
201
|
+
<pre>
|
202
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 473</span>
|
203
|
+
473: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">each_eav_relation</span>
|
204
|
+
474: <span class="ruby-identifier">eav_options</span>.<span class="ruby-identifier">keys</span>.<span class="ruby-identifier">each</span> {<span class="ruby-operator">|</span><span class="ruby-identifier">kls</span><span class="ruby-operator">|</span> <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">kls</span>.<span class="ruby-identifier">constantize</span>}
|
205
|
+
475: <span class="ruby-keyword kw">end</span>
|
206
|
+
</pre>
|
207
|
+
</div>
|
208
|
+
</div>
|
209
|
+
</div>
|
210
|
+
|
211
|
+
<div id="method-M000009" class="method-detail">
|
212
|
+
<a name="M000009"></a>
|
213
|
+
|
214
|
+
<div class="method-heading">
|
215
|
+
<a href="#M000009" class="method-signature">
|
216
|
+
<span class="method-name">eav_related</span><span class="method-args">(model)</span>
|
217
|
+
</a>
|
218
|
+
</div>
|
219
|
+
|
220
|
+
<div class="method-description">
|
221
|
+
<p>
|
222
|
+
Retrieve the collection of related eav attributes
|
223
|
+
</p>
|
224
|
+
<p><a class="source-toggle" href="#"
|
225
|
+
onclick="toggleCode('M000009-source');return false;">[Source]</a></p>
|
226
|
+
<div class="method-source-code" id="M000009-source">
|
227
|
+
<pre>
|
228
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 453</span>
|
229
|
+
453: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">eav_related</span>(<span class="ruby-identifier">model</span>)
|
230
|
+
454: <span class="ruby-identifier">relationship</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>][<span class="ruby-identifier">:relationship_name</span>]
|
231
|
+
455: <span class="ruby-identifier">send</span> <span class="ruby-identifier">relationship</span>
|
232
|
+
456: <span class="ruby-keyword kw">end</span>
|
233
|
+
</pre>
|
234
|
+
</div>
|
235
|
+
</div>
|
236
|
+
</div>
|
237
|
+
|
238
|
+
<div id="method-M000010" class="method-detail">
|
239
|
+
<a name="M000010"></a>
|
240
|
+
|
241
|
+
<div class="method-heading">
|
242
|
+
<a href="#M000010" class="method-signature">
|
243
|
+
<span class="method-name">exec_if_related</span><span class="method-args">(attribute_name) {|model| ...}</span>
|
244
|
+
</a>
|
245
|
+
</div>
|
246
|
+
|
247
|
+
<div class="method-description">
|
248
|
+
<p>
|
249
|
+
yield only if attribute_name is a eav_attribute
|
250
|
+
</p>
|
251
|
+
<p><a class="source-toggle" href="#"
|
252
|
+
onclick="toggleCode('M000010-source');return false;">[Source]</a></p>
|
253
|
+
<div class="method-source-code" id="M000010-source">
|
254
|
+
<pre>
|
255
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 461</span>
|
256
|
+
461: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">exec_if_related</span>(<span class="ruby-identifier">attribute_name</span>)
|
257
|
+
462: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">false</span> <span class="ruby-keyword kw">if</span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">class</span>.<span class="ruby-identifier">column_names</span>.<span class="ruby-identifier">include?</span>(<span class="ruby-identifier">attribute_name</span>)
|
258
|
+
463: <span class="ruby-identifier">each_eav_relation</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">model</span><span class="ruby-operator">|</span>
|
259
|
+
464: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">is_eav_attribute?</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">model</span>)
|
260
|
+
465: <span class="ruby-keyword kw">yield</span> <span class="ruby-identifier">model</span>
|
261
|
+
466: <span class="ruby-keyword kw">end</span>
|
262
|
+
467: <span class="ruby-keyword kw">end</span>
|
263
|
+
468: <span class="ruby-keyword kw">end</span>
|
264
|
+
</pre>
|
265
|
+
</div>
|
266
|
+
</div>
|
267
|
+
</div>
|
268
|
+
|
269
|
+
<div id="method-M000008" class="method-detail">
|
270
|
+
<a name="M000008"></a>
|
271
|
+
|
272
|
+
<div class="method-heading">
|
273
|
+
<a href="#M000008" class="method-signature">
|
274
|
+
<span class="method-name">find_related_eav_attribute</span><span class="method-args">(model, attribute_name)</span>
|
275
|
+
</a>
|
276
|
+
</div>
|
277
|
+
|
278
|
+
<div class="method-description">
|
279
|
+
<p>
|
280
|
+
Retrieve the related eav attribute object
|
281
|
+
</p>
|
282
|
+
<p><a class="source-toggle" href="#"
|
283
|
+
onclick="toggleCode('M000008-source');return false;">[Source]</a></p>
|
284
|
+
<div class="method-source-code" id="M000008-source">
|
285
|
+
<pre>
|
286
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 443</span>
|
287
|
+
443: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">find_related_eav_attribute</span>(<span class="ruby-identifier">model</span>, <span class="ruby-identifier">attribute_name</span>)
|
288
|
+
444: <span class="ruby-identifier">name_field</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>][<span class="ruby-identifier">:name_field</span>]
|
289
|
+
445: <span class="ruby-identifier">eav_related</span>(<span class="ruby-identifier">model</span>).<span class="ruby-identifier">to_a</span>.<span class="ruby-identifier">find</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">relation</span><span class="ruby-operator">|</span>
|
290
|
+
446: <span class="ruby-identifier">relation</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">name_field</span>) <span class="ruby-operator">==</span> <span class="ruby-identifier">attribute_name</span>
|
291
|
+
447: <span class="ruby-keyword kw">end</span>
|
292
|
+
448: <span class="ruby-keyword kw">end</span>
|
293
|
+
</pre>
|
294
|
+
</div>
|
295
|
+
</div>
|
296
|
+
</div>
|
297
|
+
|
298
|
+
<div id="method-M000007" class="method-detail">
|
299
|
+
<a name="M000007"></a>
|
300
|
+
|
301
|
+
<div class="method-heading">
|
302
|
+
<a href="#M000007" class="method-signature">
|
303
|
+
<span class="method-name">method_missing_with_eav_behavior</span><span class="method-args">(method_id, *args, &block)</span>
|
304
|
+
</a>
|
305
|
+
</div>
|
306
|
+
|
307
|
+
<div class="method-description">
|
308
|
+
<p>
|
309
|
+
Implements eav-attributes as if real getter/setter methods were defined.
|
310
|
+
</p>
|
311
|
+
<p><a class="source-toggle" href="#"
|
312
|
+
onclick="toggleCode('M000007-source');return false;">[Source]</a></p>
|
313
|
+
<div class="method-source-code" id="M000007-source">
|
314
|
+
<pre>
|
315
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 424</span>
|
316
|
+
424: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">method_missing_with_eav_behavior</span>(<span class="ruby-identifier">method_id</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
317
|
+
425: <span class="ruby-keyword kw">begin</span>
|
318
|
+
426: <span class="ruby-identifier">method_missing_without_eav_behavior</span>(<span class="ruby-identifier">method_id</span>, <span class="ruby-operator">*</span><span class="ruby-identifier">args</span>, <span class="ruby-operator">&</span><span class="ruby-identifier">block</span>)
|
319
|
+
427: <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">NoMethodError</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">e</span>
|
320
|
+
428: <span class="ruby-identifier">attribute_name</span> = <span class="ruby-identifier">method_id</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">sub</span>(<span class="ruby-regexp re">/\=$/</span>, <span class="ruby-value str">''</span>)
|
321
|
+
429: <span class="ruby-identifier">exec_if_related</span>(<span class="ruby-identifier">attribute_name</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">model</span><span class="ruby-operator">|</span>
|
322
|
+
430: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">method_id</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">=~</span> <span class="ruby-regexp re">/\=$/</span>
|
323
|
+
431: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">write_attribute_with_eav_behavior</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">args</span>[<span class="ruby-value">0</span>])
|
324
|
+
432: <span class="ruby-keyword kw">else</span>
|
325
|
+
433: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">read_attribute_with_eav_behavior</span>(<span class="ruby-identifier">attribute_name</span>)
|
326
|
+
434: <span class="ruby-keyword kw">end</span>
|
327
|
+
435: <span class="ruby-keyword kw">end</span>
|
328
|
+
436: <span class="ruby-identifier">raise</span> <span class="ruby-identifier">e</span>
|
329
|
+
437: <span class="ruby-keyword kw">end</span>
|
330
|
+
438: <span class="ruby-keyword kw">end</span>
|
331
|
+
</pre>
|
332
|
+
</div>
|
333
|
+
</div>
|
334
|
+
</div>
|
335
|
+
|
336
|
+
<div id="method-M000005" class="method-detail">
|
337
|
+
<a name="M000005"></a>
|
338
|
+
|
339
|
+
<div class="method-heading">
|
340
|
+
<a href="#M000005" class="method-signature">
|
341
|
+
<span class="method-name">read_attribute_with_eav_behavior</span><span class="method-args">(attribute_name)</span>
|
342
|
+
</a>
|
343
|
+
</div>
|
344
|
+
|
345
|
+
<div class="method-description">
|
346
|
+
<p>
|
347
|
+
Overrides ActiveRecord::Base#read_attribute
|
348
|
+
</p>
|
349
|
+
<p><a class="source-toggle" href="#"
|
350
|
+
onclick="toggleCode('M000005-source');return false;">[Source]</a></p>
|
351
|
+
<div class="method-source-code" id="M000005-source">
|
352
|
+
<pre>
|
353
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 375</span>
|
354
|
+
375: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">read_attribute_with_eav_behavior</span>(<span class="ruby-identifier">attribute_name</span>)
|
355
|
+
376: <span class="ruby-identifier">attribute_name</span> = <span class="ruby-identifier">attribute_name</span>.<span class="ruby-identifier">to_s</span>
|
356
|
+
377: <span class="ruby-identifier">exec_if_related</span>(<span class="ruby-identifier">attribute_name</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">model</span><span class="ruby-operator">|</span>
|
357
|
+
378: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-operator">!</span><span class="ruby-ivar">@remove_eav_attr</span>.<span class="ruby-identifier">nil?</span> <span class="ruby-operator">&&</span> <span class="ruby-ivar">@remove_eav_attr</span>.<span class="ruby-identifier">any?</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">r</span><span class="ruby-operator">|</span>
|
358
|
+
379: <span class="ruby-identifier">r</span>[<span class="ruby-value">0</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">model</span> <span class="ruby-operator">&&</span> <span class="ruby-identifier">r</span>[<span class="ruby-value">1</span>] <span class="ruby-operator">==</span> <span class="ruby-identifier">attribute_name</span>
|
359
|
+
380: <span class="ruby-keyword kw">end</span>
|
360
|
+
381:
|
361
|
+
382: <span class="ruby-identifier">value_field</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>][<span class="ruby-identifier">:value_field</span>]
|
362
|
+
383: <span class="ruby-identifier">related_attribute</span> = <span class="ruby-identifier">find_related_eav_attribute</span>(<span class="ruby-identifier">model</span>, <span class="ruby-identifier">attribute_name</span>)
|
363
|
+
384:
|
364
|
+
385: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">nil</span> <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">nil?</span>
|
365
|
+
386: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">value_field</span>)
|
366
|
+
387: <span class="ruby-keyword kw">end</span>
|
367
|
+
388: <span class="ruby-identifier">read_attribute_without_eav_behavior</span>(<span class="ruby-identifier">attribute_name</span>)
|
368
|
+
389: <span class="ruby-keyword kw">end</span>
|
369
|
+
</pre>
|
370
|
+
</div>
|
371
|
+
</div>
|
372
|
+
</div>
|
373
|
+
|
374
|
+
<div id="method-M000004" class="method-detail">
|
375
|
+
<a name="M000004"></a>
|
376
|
+
|
377
|
+
<div class="method-heading">
|
378
|
+
<a href="#M000004" class="method-signature">
|
379
|
+
<span class="method-name">save_modified_eav_attributes</span><span class="method-args">()</span>
|
380
|
+
</a>
|
381
|
+
</div>
|
382
|
+
|
383
|
+
<div class="method-description">
|
384
|
+
<p>
|
385
|
+
Called after validation on update so that eav attributes behave like normal
|
386
|
+
attributes in the fact that the database is not touched until save is
|
387
|
+
called.
|
388
|
+
</p>
|
389
|
+
<p><a class="source-toggle" href="#"
|
390
|
+
onclick="toggleCode('M000004-source');return false;">[Source]</a></p>
|
391
|
+
<div class="method-source-code" id="M000004-source">
|
392
|
+
<pre>
|
393
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 356</span>
|
394
|
+
356: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">save_modified_eav_attributes</span>
|
395
|
+
357: <span class="ruby-keyword kw">return</span> <span class="ruby-keyword kw">if</span> <span class="ruby-ivar">@save_eav_attr</span>.<span class="ruby-identifier">nil?</span>
|
396
|
+
358: <span class="ruby-ivar">@save_eav_attr</span>.<span class="ruby-identifier">each</span> <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">s</span><span class="ruby-operator">|</span>
|
397
|
+
359: <span class="ruby-identifier">model</span>, <span class="ruby-identifier">attribute_name</span> = <span class="ruby-identifier">s</span>
|
398
|
+
360: <span class="ruby-identifier">related_attribute</span> = <span class="ruby-identifier">find_related_eav_attribute</span>(<span class="ruby-identifier">model</span>, <span class="ruby-identifier">attribute_name</span>)
|
399
|
+
361: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">nil?</span>
|
400
|
+
362: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">value</span>.<span class="ruby-identifier">nil?</span>
|
401
|
+
363: <span class="ruby-identifier">eav_related</span>(<span class="ruby-identifier">model</span>).<span class="ruby-identifier">delete</span>(<span class="ruby-identifier">related_attribute</span>)
|
402
|
+
364: <span class="ruby-keyword kw">else</span>
|
403
|
+
365: <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">save</span>
|
404
|
+
366: <span class="ruby-keyword kw">end</span>
|
405
|
+
367: <span class="ruby-keyword kw">end</span>
|
406
|
+
368: <span class="ruby-keyword kw">end</span>
|
407
|
+
369: <span class="ruby-ivar">@save_eav_attr</span> = []
|
408
|
+
370: <span class="ruby-keyword kw">end</span>
|
409
|
+
</pre>
|
410
|
+
</div>
|
411
|
+
</div>
|
412
|
+
</div>
|
413
|
+
|
414
|
+
<div id="method-M000006" class="method-detail">
|
415
|
+
<a name="M000006"></a>
|
416
|
+
|
417
|
+
<div class="method-heading">
|
418
|
+
<a href="#M000006" class="method-signature">
|
419
|
+
<span class="method-name">write_attribute_with_eav_behavior</span><span class="method-args">(attribute_name, value)</span>
|
420
|
+
</a>
|
421
|
+
</div>
|
422
|
+
|
423
|
+
<div class="method-description">
|
424
|
+
<p>
|
425
|
+
Overrides ActiveRecord::Base#write_attribute
|
426
|
+
</p>
|
427
|
+
<p><a class="source-toggle" href="#"
|
428
|
+
onclick="toggleCode('M000006-source');return false;">[Source]</a></p>
|
429
|
+
<div class="method-source-code" id="M000006-source">
|
430
|
+
<pre>
|
431
|
+
<span class="ruby-comment cmt"># File lib/acts_as_eav_model.rb, line 394</span>
|
432
|
+
394: <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">write_attribute_with_eav_behavior</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">value</span>)
|
433
|
+
395: <span class="ruby-identifier">attribute_name</span> = <span class="ruby-identifier">attribute_name</span>.<span class="ruby-identifier">to_s</span>
|
434
|
+
396: <span class="ruby-identifier">exec_if_related</span>(<span class="ruby-identifier">attribute_name</span>) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">model</span><span class="ruby-operator">|</span>
|
435
|
+
397: <span class="ruby-identifier">value_field</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>][<span class="ruby-identifier">:value_field</span>]
|
436
|
+
398: <span class="ruby-ivar">@save_eav_attr</span> <span class="ruby-operator">||=</span> []
|
437
|
+
399: <span class="ruby-ivar">@save_eav_attr</span> <span class="ruby-operator"><<</span> [<span class="ruby-identifier">model</span>, <span class="ruby-identifier">attribute_name</span>]
|
438
|
+
400: <span class="ruby-identifier">related_attribute</span> = <span class="ruby-identifier">find_related_eav_attribute</span>(<span class="ruby-identifier">model</span>, <span class="ruby-identifier">attribute_name</span>)
|
439
|
+
401: <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">nil?</span>
|
440
|
+
402: <span class="ruby-comment cmt"># Used to check for nil? but this caused validation</span>
|
441
|
+
403: <span class="ruby-comment cmt"># problems that are harder to solve. blank? is probably</span>
|
442
|
+
404: <span class="ruby-comment cmt"># not correct but it works well for now.</span>
|
443
|
+
405: <span class="ruby-keyword kw">unless</span> <span class="ruby-identifier">value</span>.<span class="ruby-identifier">blank?</span>
|
444
|
+
406: <span class="ruby-identifier">name_field</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>][<span class="ruby-identifier">:name_field</span>]
|
445
|
+
407: <span class="ruby-identifier">foreign_key</span> = <span class="ruby-identifier">eav_options</span>[<span class="ruby-identifier">model</span>.<span class="ruby-identifier">name</span>][<span class="ruby-identifier">:foreign_key</span>]
|
446
|
+
408: <span class="ruby-identifier">eav_related</span>(<span class="ruby-identifier">model</span>).<span class="ruby-identifier">build</span> <span class="ruby-identifier">name_field</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">attribute_name</span>,
|
447
|
+
409: <span class="ruby-identifier">value_field</span> =<span class="ruby-operator">></span> <span class="ruby-identifier">value</span>, <span class="ruby-identifier">foreign_key</span> =<span class="ruby-operator">></span> <span class="ruby-keyword kw">self</span>.<span class="ruby-identifier">id</span>
|
448
|
+
410: <span class="ruby-keyword kw">end</span>
|
449
|
+
411: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">value</span>
|
450
|
+
412: <span class="ruby-keyword kw">else</span>
|
451
|
+
413: <span class="ruby-identifier">value_field</span> = (<span class="ruby-identifier">value_field</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-value str">'='</span>).<span class="ruby-identifier">to_sym</span>
|
452
|
+
414: <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">related_attribute</span>.<span class="ruby-identifier">send</span>(<span class="ruby-identifier">value_field</span>, <span class="ruby-identifier">value</span>)
|
453
|
+
415: <span class="ruby-keyword kw">end</span>
|
454
|
+
416: <span class="ruby-keyword kw">end</span>
|
455
|
+
417: <span class="ruby-identifier">write_attribute_without_eav_behavior</span>(<span class="ruby-identifier">attribute_name</span>, <span class="ruby-identifier">value</span>)
|
456
|
+
418: <span class="ruby-keyword kw">end</span>
|
457
|
+
</pre>
|
458
|
+
</div>
|
459
|
+
</div>
|
460
|
+
</div>
|
461
|
+
|
462
|
+
|
463
|
+
</div>
|
464
|
+
|
465
|
+
|
466
|
+
</div>
|
467
|
+
|
468
|
+
|
469
|
+
<div id="validator-badges">
|
470
|
+
<p><small><a href="http://validator.w3.org/check/referer">[Validate]</a></small></p>
|
471
|
+
</div>
|
472
|
+
|
473
|
+
</body>
|
474
|
+
</html>
|