xli-hickey 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGES ADDED
@@ -0,0 +1 @@
1
+ = Hickey Changelog
data/LICENSE.TXT ADDED
@@ -0,0 +1,203 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
203
+
data/README ADDED
@@ -0,0 +1,115 @@
1
+ = Hickey
2
+
3
+ Supporting Hickey version: 0.0.x
4
+
5
+ Hickey provides a simple way of preparing test data inside your test for Rails project.
6
+ For making the process of preparing test data fast, Hickey will bypass all model validations
7
+ and callbacks. Of course, your can enable them when you need.
8
+
9
+ == Installation
10
+
11
+ === GEM Installation
12
+
13
+ Run the following if you haven't already:
14
+
15
+ gem sources -a http://gems.github.com
16
+
17
+ Install the gem:
18
+
19
+ sudo gem install xli-hickey
20
+
21
+ == Simple Examples
22
+
23
+ === Environment
24
+
25
+ After installed hickey gem, require 'hickey' in your test environment.
26
+
27
+ === Create single model
28
+
29
+ Suppose we have a User model that has attributes login and admin
30
+ % class User < ActiveRecord::Base
31
+ % end
32
+
33
+ We can create a user model by the following code:
34
+ % Hickey.kiss(:user => {:login => 'xli', :admin => true})
35
+
36
+ It's not the simplest way to create user model, but just let you know how it works.
37
+
38
+ === Create domain models
39
+
40
+ Suppose we have the following models:
41
+ % class Project < ActiveRecord::Base
42
+ % has_many :tags
43
+ % has_many :cards
44
+ % end
45
+ %
46
+ % class Tag < ActiveRecord::Base
47
+ % has_many :taggings, :class_name => '::Tagging'
48
+ % belongs_to :project
49
+ % end
50
+ %
51
+ % class Tagging < ActiveRecord::Base
52
+ % belongs_to :tag
53
+ % belongs_to :taggable, :polymorphic => true
54
+ % end
55
+ %
56
+ % class Card < ActiveRecord::Base
57
+ % has_many :taggings, :as => :taggable
58
+ % belongs_to :project
59
+ % end
60
+
61
+ We can create a domain in the test by the following code:
62
+ % Hickey.kiss :project => {
63
+ % :identifier => 'hickey', :cards => [
64
+ % {
65
+ % :name => 'first card',
66
+ % :taggings => [{:tag => {:name => 'first_tag'}}],
67
+ % },
68
+ % {
69
+ % :name => 'dont make me think',
70
+ % :taggings => [{:tag => {:name => 'ignore me'}}]
71
+ % }
72
+ % ]
73
+ % }
74
+
75
+ Another example for we want to create tag cards with one same tag named 'first_tag':
76
+ % Hickey.kiss :project => {
77
+ % :identifier => 'hickey', :cards => [
78
+ % {
79
+ % :name => 'first card',
80
+ % :taggings => [{:tag => {:find_or_create => {:name => 'first_tag'}}}],
81
+ % },
82
+ % {
83
+ % :name => 'dont make me think',
84
+ % :taggings => [{:tag => {:find_or_create => {:name => 'first_tag'}}}]
85
+ % }
86
+ % ]
87
+ % }
88
+
89
+ == License
90
+
91
+ Hickey is available under an Apache License Version 2.0.
92
+
93
+ == Support
94
+
95
+ Feel free to submit commits or feature requests. If you send a patch,
96
+ remember to update the corresponding unit tests. If fact, I prefer
97
+ new feature to be submitted in the form of new unit tests.
98
+
99
+ For other information, feel free to ask on the ruby-talk mailing list or contact
100
+ mailto:iam@li-xiao.com.
101
+
102
+ = Other stuff
103
+
104
+ Author:: Li Xiao <iam@li-xiao.com>
105
+ Requires:: Ruby 1.8.6 or later
106
+ License:: Copyright 2008 by Li Xiao.
107
+ Released under an Apache License Version 2.0. See the LICENSE file
108
+ included in the distribution.
109
+
110
+ == Warranty
111
+
112
+ This software is provided "as is" and without any express or
113
+ implied warranties, including, without limitation, the implied
114
+ warranties of merchantibility and fitness for a particular
115
+ purpose.
data/Rakefile ADDED
@@ -0,0 +1,97 @@
1
+ # Rakefile for Hickey -*- ruby -*-
2
+
3
+ # Copyright 2007 by Li Xiao (iam@li-xiao.com)
4
+ # All rights reserved.
5
+
6
+ $LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/lib')
7
+ require 'hickey'
8
+
9
+ require 'rake/clean'
10
+ require 'rake/testtask'
11
+ require 'rake/rdoctask'
12
+
13
+ begin
14
+ require 'rubygems'
15
+ require 'rake/gempackagetask'
16
+ rescue Exception
17
+ nil
18
+ end
19
+
20
+ # The default task is run if rake is given no explicit arguments.
21
+
22
+ desc "Default Task"
23
+ task :default => :test_all
24
+
25
+ # Common Abbreviations ...
26
+
27
+ task :test_all => [:test_units]
28
+ task :test => :test_units
29
+
30
+ Rake::TestTask.new(:test_units) do |t|
31
+ t.test_files = FileList['test/*test.rb']
32
+ t.warning = false
33
+ t.verbose = false
34
+ end
35
+
36
+ # Create a task to build the RDOC documentation tree.
37
+
38
+ rd = Rake::RDocTask.new("rdoc") { |rdoc|
39
+ rdoc.rdoc_dir = 'html'
40
+ # rdoc.template = 'kilmer'
41
+ # rdoc.template = 'css2'
42
+ rdoc.template = 'doc/jamis.rb'
43
+ rdoc.title = "Hickey"
44
+ rdoc.options << '--line-numbers' << '--inline-source' <<
45
+ '--main' << 'README' <<
46
+ '--title' << 'Hickey'
47
+ rdoc.rdoc_files.include('README', 'LICENSE.txt', 'TODO', 'CHANGES')
48
+ rdoc.rdoc_files.include('lib/**/*.rb', 'doc/**/*.rdoc')
49
+ }
50
+
51
+ if ! defined?(Gem)
52
+ puts "Package Target requires RubyGEMs"
53
+ else
54
+ File.open(File.dirname(__FILE__) + '/hickey.gemspec') do |f|
55
+ data = f.read
56
+ spec = nil
57
+ Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
58
+ package_task = Rake::GemPackageTask.new(spec) do |pkg|
59
+ #pkg.need_zip = true
60
+ #pkg.need_tar = true
61
+ end
62
+ end
63
+ end
64
+
65
+ # Misc tasks =========================================================
66
+
67
+ def count_lines(filename)
68
+ lines = 0
69
+ codelines = 0
70
+ open(filename) { |f|
71
+ f.each do |line|
72
+ lines += 1
73
+ next if line =~ /^\s*$/
74
+ next if line =~ /^\s*#/
75
+ codelines += 1
76
+ end
77
+ }
78
+ [lines, codelines]
79
+ end
80
+
81
+ def show_line(msg, lines, loc)
82
+ printf "%6s %6s %s\n", lines.to_s, loc.to_s, msg
83
+ end
84
+
85
+ desc "Count lines in the main MM file"
86
+ task :lines do
87
+ total_lines = 0
88
+ total_code = 0
89
+ show_line("File Name", "LINES", "LOC")
90
+ FileList['lib/**/*.rb', 'lib/**/*.rake'].each do |fn|
91
+ lines, codelines = count_lines(fn)
92
+ show_line(fn, lines, codelines)
93
+ total_lines += lines
94
+ total_code += codelines
95
+ end
96
+ show_line("TOTAL", total_lines, total_code)
97
+ end
data/TODO ADDED
@@ -0,0 +1,53 @@
1
+ = Hickey Project -- To Do List
2
+
3
+ Send suggestions for this list to iam@li-xiao.com
4
+
5
+ === To Do
6
+ * auto create model associations by each_card_type:
7
+ property_definitions_card_types => [:each_card_type => {}]
8
+ * specify item type by key name inside array list example: property_definitions => [{:enum_prop_def => {:name => 'xx'}}]
9
+ * with scope
10
+ * scope with user defined foreign_key id inside of model
11
+ * Single table inheritance
12
+ * has one
13
+ * belongs_to
14
+ * has_and_belongs_to_many
15
+ * log of creating domain progress for problem detecting
16
+ * auto_generate_position_for_model_acts_as_list
17
+ * prepare multi-models domain
18
+ * :source, :source_type options in has_many :through
19
+ * enable validations
20
+ * composed_of
21
+ * prepare with model validation
22
+ * should be in transaction?
23
+ * fast prepare by sql? really need it?
24
+ * enable callbacks
25
+ * enable specific method of callbacks?
26
+ === Done
27
+ * prepare model data should not trigger any callback
28
+ * prepare model with attributes
29
+ * prepare multi-models domain
30
+ * prepare has_many relationship models
31
+ * prepare belongs_to relationship models
32
+ * prepare belongs_and_has_many relationship models
33
+ * specify class by :class_name
34
+ * has_one
35
+ * has_and_belongs_to_many
36
+ * has_many
37
+ * belongs_to
38
+ * :through in has_one
39
+ * polymorphic
40
+ * foreign_key
41
+ * Single table inheritance
42
+ * has many
43
+ * enable callbacks
44
+ * enable all callbacks
45
+ * enable specific callbacks
46
+ * actions: find or create
47
+ * find
48
+ * create as default
49
+ * find_or_create
50
+ * with scope
51
+ * simple scope with default model foreign_key id inside of model
52
+
53
+ (moved DONE list to CHANGES file)
data/hickey.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ Gem::Specification.new do |s|
2
+
3
+ #### Basic information.
4
+
5
+ s.name = 'hickey'
6
+ s.version = '0.0.1'
7
+ s.summary = "Hickey provides a simple way of preparing test data inside test for Rails project."
8
+
9
+ s.add_dependency('activerecord', '>= 2.1.0')
10
+ #### Which files are to be included in this gem? Everything! (Except SVN directories.)
11
+ # p Dir.glob("lib/**/*.rb") + Dir.glob("test/**/*.rb")
12
+ s.files = ["lib/hickey/acceptor.rb", "lib/hickey/domain_detector/actions.rb", "lib/hickey/domain_detector/associations.rb", "lib/hickey/domain_detector/base.rb", "lib/hickey/domain_detector/configurable.rb", "lib/hickey/domain_detector/scopes.rb", "lib/hickey/domain_detector.rb", "lib/hickey.rb", "test/belongs_to_association_test.rb", "test/database_config.rb", "test/enable_callbacks_test.rb", "test/find_or_create_actions_test.rb", "test/has_and_belongs_to_many_association_test.rb", "test/has_many_association_test.rb", "test/has_one_association_test.rb", "test/model_association_test.rb", "test/models/address.rb", "test/models/author.rb", "test/models/country.rb", "test/models/project.rb", "test/models/projects_member.rb", "test/models/property_definition.rb", "test/models/simple.rb", "test/models/tag.rb", "test/models/topic.rb", "test/models/user.rb", "test/models/writer.rb", "test/schema.rb", "test/single_model_test.rb", "test/test_helper.rb", "test/with_scope_test.rb"] + ["CHANGES", "hickey.gemspec", "LICENSE.TXT", "Rakefile", "README", "TODO"]
13
+
14
+ #### Load-time details: library and application (you will need one or both).
15
+
16
+ s.require_path = 'lib' # Use these for libraries.
17
+
18
+ #### Documentation and testing.
19
+
20
+ s.has_rdoc = false
21
+
22
+ #### Author and project details.
23
+ s.author = "Li Xiao"
24
+ s.email = "iam@li-xiao.com"
25
+ s.homepage = "https://github.com/xli/hickey/tree"
26
+ end
@@ -0,0 +1,25 @@
1
+ module Hickey
2
+ module Acceptor
3
+ module Hash
4
+ def accept_for_hickey(klass, visitor)
5
+ visitor.visit_hash(klass, self)
6
+ end
7
+ end
8
+ module Array
9
+ def accept_for_hickey(klass, visitor)
10
+ collect do |o|
11
+ o.accept_for_hickey(klass, visitor)
12
+ end
13
+ end
14
+ end
15
+ module ReturnSelf
16
+ def accept_for_hickey(klass, visitor)
17
+ self
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ Array.send(:include, Hickey::Acceptor::Array)
24
+ Hash.send(:include, Hickey::Acceptor::Hash)
25
+ ActiveRecord::Base.send(:include, Hickey::Acceptor::ReturnSelf)
@@ -0,0 +1,38 @@
1
+ module Hickey
2
+ module DomainDetector
3
+ module Actions
4
+ def self.included(base)
5
+ base.send(:alias_method_chain, :visit_hash, :find_or_create_actions)
6
+ end
7
+
8
+ def visit_hash_with_find_or_create_actions(attribute, record)
9
+ case record.keys.first
10
+ when :find
11
+ record = record[:find]
12
+ attribute = find_action(attribute, record)
13
+ when :create
14
+ record = record[:create]
15
+ when :find_or_create
16
+ record = record[:find_or_create]
17
+ if find_attribute = find_action(attribute, record)
18
+ attribute = find_attribute
19
+ end
20
+ end
21
+ visit_hash_without_find_or_create_actions(attribute, record)
22
+ end
23
+
24
+ def find_action(attribute, record)
25
+ model_type = compute_type(attribute, record)
26
+ conditions = record.inject({}) do |c, entity|
27
+ key, value = entity
28
+ unless [Hash, Array].include?(value.class)
29
+ #added if to filter column name for fixing hickey on acitverecord 1.2.6
30
+ c[key] = value if model_type.column_names.include?(key.to_s)
31
+ end
32
+ c
33
+ end
34
+ compute_type(attribute, record).find(:first, :conditions => conditions)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,80 @@
1
+ module Hickey
2
+ module DomainDetector
3
+ module BelongsToAssociation
4
+ def belongs_to(owner, reflection, attr_value)
5
+ association = attr_value.accept_for_hickey(reflection.klass, self)
6
+ owner.send("#{reflection.primary_key_name}=", association.id)
7
+ Proc.new{}
8
+ end
9
+ end
10
+
11
+ module HasOneAssociation
12
+ def has_one(owner, reflection, attr_value)
13
+ return has_one_through(owner, reflection, attr_value) if reflection.through_reflection
14
+
15
+ Proc.new do
16
+ attr_value[reflection.primary_key_name] = owner.id
17
+ if reflection.options[:as]
18
+ attr_value["#{reflection.options[:as]}_type"] = owner.class.base_class.name.to_s
19
+ end
20
+ attr_value.accept_for_hickey(reflection.klass, self)
21
+ end
22
+ end
23
+ end
24
+
25
+ module HasOneThroughAssociation
26
+ def has_one_through(owner, reflection, attr_value)
27
+ through_reflection = reflection.through_reflection
28
+ Proc.new do
29
+ target = attr_value.accept_for_hickey(reflection.klass, self)
30
+ association = ActiveRecord::Associations::HasOneThroughAssociation.new(owner, reflection).send(:construct_join_attributes, target)
31
+ association.accept_for_hickey(through_reflection.klass, self)
32
+ end
33
+ end
34
+ end
35
+
36
+ module HasManyAssociation
37
+ def has_many(owner, reflection, attr_value)
38
+ return has_many_through(owner, reflection, attr_value) if reflection.through_reflection
39
+
40
+ Proc.new do
41
+ attr_value.each do |obj|
42
+ obj[reflection.primary_key_name] = owner.id
43
+ if reflection.options[:as]
44
+ obj["#{reflection.options[:as]}_type"] = owner.class.base_class.name.to_s
45
+ end
46
+ end
47
+ attr_value.accept_for_hickey(reflection.klass, self)
48
+ end
49
+ end
50
+ end
51
+
52
+ module HasManyThroughAssociation
53
+ def has_many_through(owner, reflection, attr_value)
54
+ through_reflection = reflection.through_reflection
55
+ Proc.new do
56
+ owner_associations = owner.send(reflection.name)
57
+ target = attr_value.accept_for_hickey(reflection.klass, self)
58
+ target.each do |record|
59
+ association = owner_associations.send(:construct_join_attributes, record)
60
+ proxy = through_reflection.klass.send(:with_scope, :create => association) do
61
+ visit_hash(through_reflection.klass, {})
62
+ end
63
+ owner_associations.proxy_target << proxy
64
+ end
65
+ end
66
+ end
67
+ end
68
+
69
+ module HasAndBelongsToManyAssociation
70
+ def has_and_belongs_to_many(owner, reflection, attr_value)
71
+ association = attr_value.accept_for_hickey(reflection.klass, self)
72
+ Proc.new do
73
+ association.each do |record|
74
+ owner.send(reflection.name).send(:insert_record, record)
75
+ end
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end