logical_model 0.5.8 → 0.5.10
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/README.rdoc +2 -0
- data/VERSION +1 -1
- data/lib/logical_model.rb +2 -4
- data/lib/logical_model/associations.rb +11 -0
- data/lib/logical_model/associations/belongs_to.rb +67 -0
- data/lib/logical_model/associations/has_many_keys.rb +83 -0
- data/lib/logical_model/rest_actions.rb +14 -3
- data/lib/logical_model/url_helper.rb +4 -5
- data/logical_model.gemspec +5 -4
- data/spec/client_spec.rb +57 -40
- metadata +6 -5
- data/lib/logical_model/belongs_to.rb +0 -65
- data/lib/logical_model/has_many_keys.rb +0 -81
data/README.rdoc
CHANGED
@@ -9,6 +9,8 @@ It was written following this tutorial: http://www.slideshare.net/ihower/service
|
|
9
9
|
|
10
10
|
== Assumptions
|
11
11
|
|
12
|
+
OUTDATED -- some of this assumpitions are now configurable. Will update this readme as soon as possible.
|
13
|
+
|
12
14
|
LogicalModel makes some assumptions about the service you'll be consuming
|
13
15
|
|
14
16
|
* resource service is RESTfull
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.5.
|
1
|
+
0.5.10
|
data/lib/logical_model.rb
CHANGED
@@ -9,8 +9,7 @@ require 'logical_model/responses_configuration'
|
|
9
9
|
require 'logical_model/rest_actions'
|
10
10
|
require 'logical_model/url_helper'
|
11
11
|
require 'logical_model/safe_log'
|
12
|
-
require 'logical_model/
|
13
|
-
require 'logical_model/belongs_to'
|
12
|
+
require 'logical_model/associations'
|
14
13
|
require 'logical_model/api_key'
|
15
14
|
require 'logical_model/attributes'
|
16
15
|
|
@@ -59,8 +58,7 @@ class LogicalModel
|
|
59
58
|
include LogicalModel::UrlHelper
|
60
59
|
include LogicalModel::ApiKey
|
61
60
|
include LogicalModel::SafeLog
|
62
|
-
include LogicalModel::
|
63
|
-
include LogicalModel::BelongsTo
|
61
|
+
include LogicalModel::Associations
|
64
62
|
|
65
63
|
# include ActiveModel Modules that are usefull
|
66
64
|
extend ActiveModel::Naming
|
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'logical_model/associations/has_many_keys'
|
2
|
+
require 'logical_model/associations/belongs_to'
|
3
|
+
|
4
|
+
class LogicalModel
|
5
|
+
module Associations
|
6
|
+
def self.included(base)
|
7
|
+
base.send :include, LogicalModel::Associations::HasManyKeys
|
8
|
+
base.send :include, LogicalModel::Associations::BelongsTo
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,67 @@
|
|
1
|
+
class LogicalModel
|
2
|
+
module Associations
|
3
|
+
module BelongsTo
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:extend, ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
# @param key [String] association name
|
11
|
+
# @param options [Hash]
|
12
|
+
# @option options [String/Constant] class
|
13
|
+
def belongs_to(key, options = {})
|
14
|
+
attr_accessor "#{key}_id"
|
15
|
+
attr_class = get_attr_class(key, options)
|
16
|
+
|
17
|
+
define_method("#{key}=") do |param|
|
18
|
+
if param.is_a?(Hash)
|
19
|
+
param.stringify_keys!
|
20
|
+
instance_variable_set("@#{key}_id", param['id']) if param['id']
|
21
|
+
instance = attr_class.new(param)
|
22
|
+
elsif param.is_a?(attr_class)
|
23
|
+
instance_variable_set("@#{key}_id", param.id)
|
24
|
+
instance = param
|
25
|
+
else
|
26
|
+
# ...
|
27
|
+
end
|
28
|
+
|
29
|
+
instance_variable_set("@#{key}",instance)
|
30
|
+
end
|
31
|
+
|
32
|
+
define_method(key) do
|
33
|
+
instance = eval("@#{key}")
|
34
|
+
if instance.nil?
|
35
|
+
instance = attr_class.find(eval("#{key}_id"))
|
36
|
+
instance_variable_set("@#{key}",instance)
|
37
|
+
end
|
38
|
+
instance
|
39
|
+
end
|
40
|
+
|
41
|
+
# TODO define_method("#{key}_attribute="){|param| ... }
|
42
|
+
|
43
|
+
define_method "new_#{key}" do |param|
|
44
|
+
attr_class
|
45
|
+
|
46
|
+
return unless attr_class
|
47
|
+
|
48
|
+
temp_object = attr_class.new(param.merge({"#{self.json_root}_id" => self.id}))
|
49
|
+
eval(key.to_s) << temp_object
|
50
|
+
temp_object
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def get_attr_class(key, options)
|
57
|
+
if options[:class]
|
58
|
+
attr_class = options[:class].is_a?(String) ? options[:class].constantize : options[:class]
|
59
|
+
else
|
60
|
+
attr_class = key.to_s.singularize.camelize.constantize
|
61
|
+
end
|
62
|
+
attr_class
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,83 @@
|
|
1
|
+
class LogicalModel
|
2
|
+
module Associations
|
3
|
+
module HasManyKeys
|
4
|
+
|
5
|
+
def self.included(base)
|
6
|
+
base.send(:extend, ClassMethods)
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def has_many(key)
|
12
|
+
@has_many_keys ||= []
|
13
|
+
@has_many_keys << key
|
14
|
+
has_many_keys = @has_many_keys
|
15
|
+
end
|
16
|
+
|
17
|
+
def has_many_keys=(keys)
|
18
|
+
@has_many_keys = keys
|
19
|
+
attr_accessor *keys
|
20
|
+
|
21
|
+
keys.each do |association|
|
22
|
+
|
23
|
+
# return empty array or @association variable for each association
|
24
|
+
define_method association do
|
25
|
+
if instance_variable_get("@#{association}").blank?
|
26
|
+
instance_variable_set("@#{association}", [])
|
27
|
+
end
|
28
|
+
|
29
|
+
instance_variable_get("@#{association}")
|
30
|
+
end
|
31
|
+
|
32
|
+
# this method loads the contact attributes recieved by logical model from the service
|
33
|
+
define_method "#{association}=" do |params|
|
34
|
+
collection = []
|
35
|
+
params.each do |attr_params|
|
36
|
+
if attr_params["_type"].present?
|
37
|
+
attr_class = attr_params.delete("_type").to_s.constantize
|
38
|
+
else
|
39
|
+
attr_class = association.to_s.singularize.camelize.constantize
|
40
|
+
end
|
41
|
+
collection << attr_class.new(attr_params)
|
42
|
+
end
|
43
|
+
instance_variable_set("@#{association}", collection)
|
44
|
+
end
|
45
|
+
|
46
|
+
define_method "new_#{association.to_s.singularize}" do |attr_params|
|
47
|
+
if attr_params["_type"].present?
|
48
|
+
clazz = attr_params.delete(:_type).constantize
|
49
|
+
else
|
50
|
+
clazz = association.to_s.singularize.camelize.constantize
|
51
|
+
end
|
52
|
+
|
53
|
+
return unless clazz
|
54
|
+
|
55
|
+
temp_object = clazz.new(attr_params.merge({"#{self.json_root}_id" => self.id}))
|
56
|
+
eval(association.to_s) << temp_object
|
57
|
+
temp_object
|
58
|
+
end
|
59
|
+
|
60
|
+
# this method loads the contact attributes from the html form (using nested resources conventions)
|
61
|
+
define_method "#{association}_attributes=" do |key_attributes|
|
62
|
+
array = []
|
63
|
+
key_attributes.each do |attr_params|
|
64
|
+
attr_params.to_hash.symbolize_keys!
|
65
|
+
if attr_params["_type"].present?
|
66
|
+
attr_class = attr_params.delete("_type").to_s.constantize
|
67
|
+
else
|
68
|
+
attr_class = association.to_s.singularize.camelize.constantize
|
69
|
+
end
|
70
|
+
array << attr_class.new(attr_params)
|
71
|
+
end
|
72
|
+
instance_variable_set("@#{association}", array)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
def has_many_keys
|
78
|
+
@has_many_keys
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -171,6 +171,17 @@ class LogicalModel
|
|
171
171
|
|
172
172
|
attr_accessor :enable_delete_multiple
|
173
173
|
|
174
|
+
# @param header [Hash]
|
175
|
+
def set_default_headers(header)
|
176
|
+
@headers = header
|
177
|
+
end
|
178
|
+
|
179
|
+
# User specified default headers
|
180
|
+
# @return [Hash]
|
181
|
+
def default_headers
|
182
|
+
@headers
|
183
|
+
end
|
184
|
+
|
174
185
|
# ============================================================================================================
|
175
186
|
# Following methods are API specific.
|
176
187
|
# They assume we are using a RESTfull API.
|
@@ -184,7 +195,7 @@ class LogicalModel
|
|
184
195
|
# @param options [Hash] will be forwarded to API
|
185
196
|
def async_all(options={})
|
186
197
|
options = self.merge_key(options)
|
187
|
-
request = Typhoeus::Request.new(resource_uri, :
|
198
|
+
request = Typhoeus::Request.new(resource_uri, params: options, headers: default_headers)
|
188
199
|
request.on_complete do |response|
|
189
200
|
if response.code >= 200 && response.code < 400
|
190
201
|
log_ok(response)
|
@@ -235,7 +246,7 @@ class LogicalModel
|
|
235
246
|
|
236
247
|
options = self.merge_key(options)
|
237
248
|
|
238
|
-
request = Typhoeus::Request.new(resource_uri, :
|
249
|
+
request = Typhoeus::Request.new(resource_uri, params: options, headers: default_headers)
|
239
250
|
request.on_complete do |response|
|
240
251
|
if response.code >= 200 && response.code < 400
|
241
252
|
log_ok(response)
|
@@ -296,7 +307,7 @@ class LogicalModel
|
|
296
307
|
|
297
308
|
options = self.merge_key(options)
|
298
309
|
|
299
|
-
request = Typhoeus::Request.new(resource_uri, :
|
310
|
+
request = Typhoeus::Request.new(resource_uri, params: options, headers: default_headers)
|
300
311
|
request.on_complete do |response|
|
301
312
|
if response.code >= 200 && response.code < 400
|
302
313
|
log_ok(response)
|
@@ -62,15 +62,14 @@ class LogicalModel
|
|
62
62
|
(use_ssl?)? "https://" : "http://"
|
63
63
|
end
|
64
64
|
|
65
|
-
# Returns true if ssl is recommended
|
65
|
+
# Returns true if ssl is recommended
|
66
66
|
#
|
67
|
-
# -
|
68
|
-
# - other ->
|
67
|
+
# - requests to localhost -> false
|
68
|
+
# - other -> true
|
69
69
|
#
|
70
70
|
# @return [Boolean]
|
71
71
|
def ssl_recommended?
|
72
|
-
|
73
|
-
ssl_recommended_environments.include?(defined?(Rails)? Rails.env : ENV['RACK_ENV'] )
|
72
|
+
(@host && !(@host =~ /localhost/))
|
74
73
|
end
|
75
74
|
|
76
75
|
# Requests done within the block will go to new path.
|
data/logical_model.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "logical_model"
|
8
|
-
s.version = "0.5.
|
8
|
+
s.version = "0.5.10"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Dwayne Macgowan"]
|
12
|
-
s.date = "2013-
|
12
|
+
s.date = "2013-11-08"
|
13
13
|
s.description = "LogicalModel allows to use a resource as a model. It is based on web presentation http://www.slideshare.net/ihower/serviceoriented-design-and-implement-with-rails3"
|
14
14
|
s.email = "dwaynemac@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -34,9 +34,10 @@ Gem::Specification.new do |s|
|
|
34
34
|
"db/migrate/001_create_users.rb",
|
35
35
|
"lib/logical_model.rb",
|
36
36
|
"lib/logical_model/api_key.rb",
|
37
|
+
"lib/logical_model/associations.rb",
|
38
|
+
"lib/logical_model/associations/belongs_to.rb",
|
39
|
+
"lib/logical_model/associations/has_many_keys.rb",
|
37
40
|
"lib/logical_model/attributes.rb",
|
38
|
-
"lib/logical_model/belongs_to.rb",
|
39
|
-
"lib/logical_model/has_many_keys.rb",
|
40
41
|
"lib/logical_model/hydra.rb",
|
41
42
|
"lib/logical_model/responses_configuration.rb",
|
42
43
|
"lib/logical_model/rest_actions.rb",
|
data/spec/client_spec.rb
CHANGED
@@ -27,42 +27,6 @@ describe "LogicalModel User client" do
|
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
|
-
describe "belongs_to :account" do
|
31
|
-
before do
|
32
|
-
class Account < LogicalModel
|
33
|
-
attribute :id
|
34
|
-
attribute :name
|
35
|
-
end
|
36
|
-
class User < LogicalModel
|
37
|
-
belongs_to :account
|
38
|
-
end
|
39
|
-
|
40
|
-
@account = Account.new(id: 1, name: 'asdf')
|
41
|
-
Account.stub(:find).with(1).and_return(@account)
|
42
|
-
end
|
43
|
-
it "responds to account_id" do
|
44
|
-
User.new.should respond_to :account_id
|
45
|
-
end
|
46
|
-
it "responds to account" do
|
47
|
-
User.new.should respond_to :account
|
48
|
-
end
|
49
|
-
it "allows to set association_id=x" do
|
50
|
-
u = User.new
|
51
|
-
u.account_id = 1
|
52
|
-
u.account.should == @account
|
53
|
-
end
|
54
|
-
it "allows to set association=Object" do
|
55
|
-
u = User.new
|
56
|
-
u.account = @account
|
57
|
-
u.account_id.should == 1
|
58
|
-
end
|
59
|
-
it "allows to set association=AttributesHash" do
|
60
|
-
u = User.new
|
61
|
-
u.account = {id: 3, name: 'account_name'}
|
62
|
-
u.account.should be_a Account
|
63
|
-
u.account.name.should == 'account_name'
|
64
|
-
end
|
65
|
-
end
|
66
30
|
|
67
31
|
describe "RESTActions" do
|
68
32
|
%W(find async_find paginate async_paginate delete delete_multiple).each do |class_action|
|
@@ -70,11 +34,53 @@ describe "LogicalModel User client" do
|
|
70
34
|
end
|
71
35
|
end
|
72
36
|
|
73
|
-
describe "
|
74
|
-
|
75
|
-
|
37
|
+
describe "associations" do
|
38
|
+
describe "belongs_to :account" do
|
39
|
+
before do
|
40
|
+
class Account < LogicalModel
|
41
|
+
attribute :id
|
42
|
+
attribute :name
|
43
|
+
end
|
44
|
+
class User < LogicalModel
|
45
|
+
belongs_to :account
|
46
|
+
end
|
47
|
+
|
48
|
+
@account = Account.new(id: 1, name: 'asdf')
|
49
|
+
Account.stub(:find).with(1).and_return(@account)
|
50
|
+
end
|
51
|
+
it "responds to account_id" do
|
52
|
+
User.new.should respond_to :account_id
|
53
|
+
end
|
54
|
+
it "responds to account" do
|
55
|
+
User.new.should respond_to :account
|
56
|
+
end
|
57
|
+
it "allows to set association_id=x" do
|
58
|
+
u = User.new
|
59
|
+
u.account_id = 1
|
60
|
+
u.account.should == @account
|
61
|
+
end
|
62
|
+
it "allows to set association=Object" do
|
63
|
+
u = User.new
|
64
|
+
u.account = @account
|
65
|
+
u.account_id.should == 1
|
66
|
+
end
|
67
|
+
it "allows to set association=AttributesHash" do
|
68
|
+
u = User.new
|
69
|
+
u.account = {id: 3, name: 'account_name'}
|
70
|
+
u.account.should be_a Account
|
71
|
+
u.account.name.should == 'account_name'
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "has_many_keys" do
|
76
|
+
it { should respond_to 'has_many_keys=' }
|
77
|
+
it { should respond_to 'has_many_keys'}
|
78
|
+
end
|
79
|
+
|
80
|
+
|
76
81
|
end
|
77
82
|
|
83
|
+
|
78
84
|
describe "url_helper" do
|
79
85
|
it { should respond_to 'use_ssl=' }
|
80
86
|
it { should respond_to 'use_ssl?' }
|
@@ -88,6 +94,17 @@ describe "LogicalModel User client" do
|
|
88
94
|
User.use_ssl?.should be_true
|
89
95
|
end
|
90
96
|
end
|
97
|
+
describe "ssl_recommended?" do
|
98
|
+
it "returns false for localhost domains" do
|
99
|
+
User.host.should == "localhost:3000"
|
100
|
+
User.ssl_recommended?.should be_false
|
101
|
+
end
|
102
|
+
it "returns true for other domains" do
|
103
|
+
class User; set_resource_host("new_host"); end;
|
104
|
+
User.host.should == "new_host"
|
105
|
+
User.ssl_recommended?.should be_true
|
106
|
+
end
|
107
|
+
end
|
91
108
|
describe "set_resource_host" do
|
92
109
|
it { should respond_to 'set_resource_host' }
|
93
110
|
it "sets resource_path" do
|
@@ -423,4 +440,4 @@ describe "LogicalModel User client" do
|
|
423
440
|
end
|
424
441
|
end
|
425
442
|
|
426
|
-
end
|
443
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: logical_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.10
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-11-08 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activemodel
|
@@ -341,9 +341,10 @@ files:
|
|
341
341
|
- db/migrate/001_create_users.rb
|
342
342
|
- lib/logical_model.rb
|
343
343
|
- lib/logical_model/api_key.rb
|
344
|
+
- lib/logical_model/associations.rb
|
345
|
+
- lib/logical_model/associations/belongs_to.rb
|
346
|
+
- lib/logical_model/associations/has_many_keys.rb
|
344
347
|
- lib/logical_model/attributes.rb
|
345
|
-
- lib/logical_model/belongs_to.rb
|
346
|
-
- lib/logical_model/has_many_keys.rb
|
347
348
|
- lib/logical_model/hydra.rb
|
348
349
|
- lib/logical_model/responses_configuration.rb
|
349
350
|
- lib/logical_model/rest_actions.rb
|
@@ -372,7 +373,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
372
373
|
version: '0'
|
373
374
|
segments:
|
374
375
|
- 0
|
375
|
-
hash:
|
376
|
+
hash: 449501073
|
376
377
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
377
378
|
none: false
|
378
379
|
requirements:
|
@@ -1,65 +0,0 @@
|
|
1
|
-
class LogicalModel
|
2
|
-
module BelongsTo
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
base.send(:extend, ClassMethods)
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
# @param key [String] association name
|
10
|
-
# @param options [Hash]
|
11
|
-
# @option options [String/Constant] class
|
12
|
-
def belongs_to(key, options = {})
|
13
|
-
attr_accessor "#{key}_id"
|
14
|
-
attr_class = get_attr_class(key, options)
|
15
|
-
|
16
|
-
define_method("#{key}=") do |param|
|
17
|
-
if param.is_a?(Hash)
|
18
|
-
param.stringify_keys!
|
19
|
-
instance_variable_set("@#{key}_id", param['id']) if param['id']
|
20
|
-
instance = attr_class.new(param)
|
21
|
-
elsif param.is_a?(attr_class)
|
22
|
-
instance_variable_set("@#{key}_id", param.id)
|
23
|
-
instance = param
|
24
|
-
else
|
25
|
-
# ...
|
26
|
-
end
|
27
|
-
|
28
|
-
instance_variable_set("@#{key}",instance)
|
29
|
-
end
|
30
|
-
|
31
|
-
define_method(key) do
|
32
|
-
instance = eval("@#{key}")
|
33
|
-
if instance.nil?
|
34
|
-
instance = attr_class.find(eval("#{key}_id"))
|
35
|
-
instance_variable_set("@#{key}",instance)
|
36
|
-
end
|
37
|
-
instance
|
38
|
-
end
|
39
|
-
|
40
|
-
# TODO define_method("#{key}_attribute="){|param| ... }
|
41
|
-
|
42
|
-
define_method "new_#{key}" do |param|
|
43
|
-
attr_class
|
44
|
-
|
45
|
-
return unless attr_class
|
46
|
-
|
47
|
-
temp_object = attr_class.new(param.merge({"#{self.json_root}_id" => self.id}))
|
48
|
-
eval(key.to_s) << temp_object
|
49
|
-
temp_object
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
private
|
54
|
-
|
55
|
-
def get_attr_class(key, options)
|
56
|
-
if options[:class]
|
57
|
-
attr_class = options[:class].is_a?(String) ? options[:class].constantize : options[:class]
|
58
|
-
else
|
59
|
-
attr_class = key.to_s.singularize.camelize.constantize
|
60
|
-
end
|
61
|
-
attr_class
|
62
|
-
end
|
63
|
-
end
|
64
|
-
end
|
65
|
-
end
|
@@ -1,81 +0,0 @@
|
|
1
|
-
class LogicalModel
|
2
|
-
module HasManyKeys
|
3
|
-
|
4
|
-
def self.included(base)
|
5
|
-
base.send(:extend, ClassMethods)
|
6
|
-
end
|
7
|
-
|
8
|
-
module ClassMethods
|
9
|
-
|
10
|
-
def has_many(key)
|
11
|
-
@has_many_keys ||= []
|
12
|
-
@has_many_keys << key
|
13
|
-
has_many_keys = @has_many_keys
|
14
|
-
end
|
15
|
-
|
16
|
-
def has_many_keys=(keys)
|
17
|
-
@has_many_keys = keys
|
18
|
-
attr_accessor *keys
|
19
|
-
|
20
|
-
keys.each do |association|
|
21
|
-
|
22
|
-
# return empty array or @association variable for each association
|
23
|
-
define_method association do
|
24
|
-
if instance_variable_get("@#{association}").blank?
|
25
|
-
instance_variable_set("@#{association}", [])
|
26
|
-
end
|
27
|
-
|
28
|
-
instance_variable_get("@#{association}")
|
29
|
-
end
|
30
|
-
|
31
|
-
# this method loads the contact attributes recieved by logical model from the service
|
32
|
-
define_method "#{association}=" do |params|
|
33
|
-
collection = []
|
34
|
-
params.each do |attr_params|
|
35
|
-
if attr_params["_type"].present?
|
36
|
-
attr_class = attr_params.delete("_type").to_s.constantize
|
37
|
-
else
|
38
|
-
attr_class = association.to_s.singularize.camelize.constantize
|
39
|
-
end
|
40
|
-
collection << attr_class.new(attr_params)
|
41
|
-
end
|
42
|
-
instance_variable_set("@#{association}", collection)
|
43
|
-
end
|
44
|
-
|
45
|
-
define_method "new_#{association.to_s.singularize}" do |attr_params|
|
46
|
-
if attr_params["_type"].present?
|
47
|
-
clazz = attr_params.delete(:_type).constantize
|
48
|
-
else
|
49
|
-
clazz = association.to_s.singularize.camelize.constantize
|
50
|
-
end
|
51
|
-
|
52
|
-
return unless clazz
|
53
|
-
|
54
|
-
temp_object = clazz.new(attr_params.merge({"#{self.json_root}_id" => self.id}))
|
55
|
-
eval(association.to_s) << temp_object
|
56
|
-
temp_object
|
57
|
-
end
|
58
|
-
|
59
|
-
# this method loads the contact attributes from the html form (using nested resources conventions)
|
60
|
-
define_method "#{association}_attributes=" do |key_attributes|
|
61
|
-
array = []
|
62
|
-
key_attributes.each do |attr_params|
|
63
|
-
attr_params.to_hash.symbolize_keys!
|
64
|
-
if attr_params["_type"].present?
|
65
|
-
attr_class = attr_params.delete("_type").to_s.constantize
|
66
|
-
else
|
67
|
-
attr_class = association.to_s.singularize.camelize.constantize
|
68
|
-
end
|
69
|
-
array << attr_class.new(attr_params)
|
70
|
-
end
|
71
|
-
instance_variable_set("@#{association}", array)
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
def has_many_keys
|
77
|
-
@has_many_keys
|
78
|
-
end
|
79
|
-
end
|
80
|
-
end
|
81
|
-
end
|