test_track_rails_client 0.9.7 → 0.9.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/README.md +11 -8
- data/lib/test_track_rails_client/version.rb +1 -1
- data/vendor/gems/fakeable_her/fakeable_her.gemspec +22 -0
- data/vendor/gems/fakeable_her/lib/fakeable_her/model.rb +148 -0
- data/vendor/gems/fakeable_her/lib/fakeable_her/version.rb +3 -0
- data/vendor/gems/fakeable_her/lib/fakeable_her.rb +5 -0
- data/vendor/gems/her/CONTRIBUTING.md +26 -0
- data/vendor/gems/her/Gemfile +10 -0
- data/vendor/gems/her/LICENSE +7 -0
- data/vendor/gems/her/README.md +1023 -0
- data/vendor/gems/her/Rakefile +11 -0
- data/vendor/gems/her/UPGRADE.md +101 -0
- data/vendor/gems/her/gemfiles/Gemfile.activemodel-3.2.x +7 -0
- data/vendor/gems/her/gemfiles/Gemfile.activemodel-4.0 +7 -0
- data/vendor/gems/her/gemfiles/Gemfile.activemodel-4.1 +7 -0
- data/vendor/gems/her/gemfiles/Gemfile.activemodel-4.2 +7 -0
- data/vendor/gems/her/her.gemspec +31 -0
- data/vendor/gems/her/lib/her/api.rb +119 -0
- data/vendor/gems/her/lib/her/collection.rb +12 -0
- data/vendor/gems/her/lib/her/error_collection.rb +15 -0
- data/vendor/gems/her/lib/her/errors.rb +40 -0
- data/vendor/gems/her/lib/her/json_api/model.rb +46 -0
- data/vendor/gems/her/lib/her/middleware/accept_json.rb +17 -0
- data/vendor/gems/her/lib/her/middleware/first_level_parse_json.rb +36 -0
- data/vendor/gems/her/lib/her/middleware/json_api_parser.rb +36 -0
- data/vendor/gems/her/lib/her/middleware/parse_json.rb +21 -0
- data/vendor/gems/her/lib/her/middleware/second_level_parse_json.rb +36 -0
- data/vendor/gems/her/lib/her/middleware.rb +12 -0
- data/vendor/gems/her/lib/her/model/active_model_overrides.rb +13 -0
- data/vendor/gems/her/lib/her/model/associations/association.rb +106 -0
- data/vendor/gems/her/lib/her/model/associations/association_proxy.rb +46 -0
- data/vendor/gems/her/lib/her/model/associations/belongs_to_association.rb +96 -0
- data/vendor/gems/her/lib/her/model/associations/has_many_association.rb +100 -0
- data/vendor/gems/her/lib/her/model/associations/has_one_association.rb +79 -0
- data/vendor/gems/her/lib/her/model/associations.rb +141 -0
- data/vendor/gems/her/lib/her/model/attributes.rb +304 -0
- data/vendor/gems/her/lib/her/model/base.rb +33 -0
- data/vendor/gems/her/lib/her/model/deprecated_methods.rb +61 -0
- data/vendor/gems/her/lib/her/model/http.rb +117 -0
- data/vendor/gems/her/lib/her/model/introspection.rb +65 -0
- data/vendor/gems/her/lib/her/model/nested_attributes.rb +45 -0
- data/vendor/gems/her/lib/her/model/orm.rb +219 -0
- data/vendor/gems/her/lib/her/model/parse.rb +215 -0
- data/vendor/gems/her/lib/her/model/paths.rb +126 -0
- data/vendor/gems/her/lib/her/model/relation.rb +251 -0
- data/vendor/gems/her/lib/her/model.rb +81 -0
- data/vendor/gems/her/lib/her/version.rb +3 -0
- data/vendor/gems/her/lib/her.rb +20 -0
- data/vendor/gems/her/spec/api_spec.rb +114 -0
- data/vendor/gems/her/spec/collection_spec.rb +26 -0
- data/vendor/gems/her/spec/error_collection_spec.rb +33 -0
- data/vendor/gems/her/spec/json_api/model_spec.rb +168 -0
- data/vendor/gems/her/spec/middleware/accept_json_spec.rb +10 -0
- data/vendor/gems/her/spec/middleware/first_level_parse_json_spec.rb +62 -0
- data/vendor/gems/her/spec/middleware/json_api_parser_spec.rb +32 -0
- data/vendor/gems/her/spec/middleware/second_level_parse_json_spec.rb +35 -0
- data/vendor/gems/her/spec/model/associations/association_proxy_spec.rb +31 -0
- data/vendor/gems/her/spec/model/associations_spec.rb +504 -0
- data/vendor/gems/her/spec/model/attributes_spec.rb +404 -0
- data/vendor/gems/her/spec/model/callbacks_spec.rb +145 -0
- data/vendor/gems/her/spec/model/dirty_spec.rb +110 -0
- data/vendor/gems/her/spec/model/http_spec.rb +165 -0
- data/vendor/gems/her/spec/model/introspection_spec.rb +76 -0
- data/vendor/gems/her/spec/model/nested_attributes_spec.rb +134 -0
- data/vendor/gems/her/spec/model/orm_spec.rb +791 -0
- data/vendor/gems/her/spec/model/parse_spec.rb +372 -0
- data/vendor/gems/her/spec/model/paths_spec.rb +347 -0
- data/vendor/gems/her/spec/model/relation_spec.rb +226 -0
- data/vendor/gems/her/spec/model/validations_spec.rb +42 -0
- data/vendor/gems/her/spec/model_spec.rb +31 -0
- data/vendor/gems/her/spec/spec_helper.rb +27 -0
- data/vendor/gems/her/spec/support/extensions/array.rb +5 -0
- data/vendor/gems/her/spec/support/extensions/hash.rb +5 -0
- data/vendor/gems/her/spec/support/macros/her_macros.rb +17 -0
- data/vendor/gems/her/spec/support/macros/model_macros.rb +36 -0
- data/vendor/gems/her/spec/support/macros/request_macros.rb +27 -0
- data/vendor/gems/publicsuffix-ruby/CHANGELOG.md +236 -0
- data/vendor/gems/publicsuffix-ruby/Gemfile +3 -0
- data/vendor/gems/publicsuffix-ruby/LICENSE.txt +22 -0
- data/vendor/gems/publicsuffix-ruby/README.md +151 -0
- data/vendor/gems/publicsuffix-ruby/Rakefile +109 -0
- data/vendor/gems/publicsuffix-ruby/lib/definitions.txt +11467 -0
- data/vendor/gems/publicsuffix-ruby/lib/public_suffix/domain.rb +387 -0
- data/vendor/gems/publicsuffix-ruby/lib/public_suffix/errors.rb +53 -0
- data/vendor/gems/publicsuffix-ruby/lib/public_suffix/list.rb +302 -0
- data/vendor/gems/publicsuffix-ruby/lib/public_suffix/rule.rb +373 -0
- data/vendor/gems/publicsuffix-ruby/lib/public_suffix/version.rb +23 -0
- data/vendor/gems/publicsuffix-ruby/lib/public_suffix.rb +131 -0
- data/vendor/gems/publicsuffix-ruby/public_suffix.gemspec +39 -0
- data/vendor/gems/publicsuffix-ruby/test/acceptance_test.rb +42 -0
- data/vendor/gems/publicsuffix-ruby/test/test_helper.rb +6 -0
- data/vendor/gems/publicsuffix-ruby/test/unit/domain_test.rb +170 -0
- data/vendor/gems/publicsuffix-ruby/test/unit/errors_test.rb +23 -0
- data/vendor/gems/publicsuffix-ruby/test/unit/list_test.rb +179 -0
- data/vendor/gems/publicsuffix-ruby/test/unit/public_suffix_test.rb +115 -0
- data/vendor/gems/publicsuffix-ruby/test/unit/rule_test.rb +307 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/capybara_configuration.rb +98 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/matchers.rb +151 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/rspec_configuration.rb +34 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/rubocop/cop/betterment/html_safe.rb +15 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/rubocop/cop/betterment/raw.rb +15 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/rubocop/cop/betterment/safe_concat.rb +15 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/rubocop.rb +3 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/shared_examples/betterment_application_examples.rb +47 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/shared_examples.rb +1 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/site_prism_configuration.rb +42 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/site_prism_dropdown.rb +17 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/version.rb +3 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers/webmock_configuration.rb +8 -0
- data/vendor/gems/ruby_spec_helpers/lib/ruby_spec_helpers.rb +2 -0
- data/vendor/gems/ruby_spec_helpers/ruby_spec_helpers.gemspec +25 -0
- metadata +110 -1
@@ -0,0 +1,106 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Associations
|
4
|
+
class Association
|
5
|
+
# @private
|
6
|
+
attr_accessor :params
|
7
|
+
|
8
|
+
# @private
|
9
|
+
def initialize(parent, opts = {})
|
10
|
+
@parent = parent
|
11
|
+
@opts = opts
|
12
|
+
@params = {}
|
13
|
+
|
14
|
+
@klass = @parent.class.her_nearby_class(@opts[:class_name])
|
15
|
+
@name = @opts[:name]
|
16
|
+
end
|
17
|
+
|
18
|
+
# @private
|
19
|
+
def self.proxy(parent, opts = {})
|
20
|
+
AssociationProxy.new new(parent, opts)
|
21
|
+
end
|
22
|
+
|
23
|
+
# @private
|
24
|
+
def self.parse_single(association, klass, data)
|
25
|
+
data_key = association[:data_key]
|
26
|
+
return {} unless data[data_key]
|
27
|
+
|
28
|
+
klass = klass.her_nearby_class(association[:class_name])
|
29
|
+
if data[data_key].kind_of?(klass)
|
30
|
+
{ association[:name] => data[data_key] }
|
31
|
+
else
|
32
|
+
resource = klass.new(klass.parse(data[data_key]))
|
33
|
+
resource.instance_variable_set(:@changed_attributes, {})
|
34
|
+
resource.run_callbacks :find
|
35
|
+
{ association[:name] => resource }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @private
|
40
|
+
def assign_single_nested_attributes(attributes)
|
41
|
+
if @parent.attributes[@name].blank?
|
42
|
+
@parent.attributes[@name] = @klass.new(@klass.parse(attributes))
|
43
|
+
else
|
44
|
+
@parent.attributes[@name].assign_attributes(attributes)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
# @private
|
49
|
+
def fetch(opts = {})
|
50
|
+
attribute_value = @parent.attributes[@name]
|
51
|
+
return @opts[:default].try(:dup) if @parent.attributes.include?(@name) && (attribute_value.nil? || !attribute_value.nil? && attribute_value.empty?) && @params.empty?
|
52
|
+
|
53
|
+
return @cached_result unless @params.any? || @cached_result.nil?
|
54
|
+
return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?
|
55
|
+
|
56
|
+
path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}" }
|
57
|
+
@klass.get(path, @params).tap do |result|
|
58
|
+
@cached_result = result unless @params.any?
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# @private
|
63
|
+
def build_association_path(code)
|
64
|
+
begin
|
65
|
+
instance_exec(&code)
|
66
|
+
rescue Her::Errors::PathError
|
67
|
+
return nil
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
# Add query parameters to the HTTP request performed to fetch the data
|
72
|
+
#
|
73
|
+
# @example
|
74
|
+
# class User
|
75
|
+
# include Her::Model
|
76
|
+
# has_many :comments
|
77
|
+
# end
|
78
|
+
#
|
79
|
+
# user = User.find(1)
|
80
|
+
# user.comments.where(:approved => 1) # Fetched via GET "/users/1/comments?approved=1
|
81
|
+
def where(params = {})
|
82
|
+
return self if params.blank? && @parent.attributes[@name].blank?
|
83
|
+
AssociationProxy.new self.clone.tap { |a| a.params = a.params.merge(params) }
|
84
|
+
end
|
85
|
+
alias all where
|
86
|
+
|
87
|
+
# Fetches the data specified by id
|
88
|
+
#
|
89
|
+
# @example
|
90
|
+
# class User
|
91
|
+
# include Her::Model
|
92
|
+
# has_many :comments
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# user = User.find(1)
|
96
|
+
# user.comments.find(3) # Fetched via GET "/users/1/comments/3
|
97
|
+
def find(id)
|
98
|
+
return nil if id.blank?
|
99
|
+
path = build_association_path lambda { "#{@parent.request_path(@params)}#{@opts[:path]}/#{id}" }
|
100
|
+
@klass.get_resource(path, @params)
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Associations
|
4
|
+
class AssociationProxy < (ActiveSupport.const_defined?('ProxyObject') ? ActiveSupport::ProxyObject : ActiveSupport::BasicObject)
|
5
|
+
|
6
|
+
# @private
|
7
|
+
def self.install_proxy_methods(target_name, *names)
|
8
|
+
names.each do |name|
|
9
|
+
module_eval <<-RUBY, __FILE__, __LINE__ + 1
|
10
|
+
def #{name}(*args, &block)
|
11
|
+
#{target_name}.send(#{name.inspect}, *args, &block)
|
12
|
+
end
|
13
|
+
RUBY
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
install_proxy_methods :association,
|
18
|
+
:build, :create, :where, :find, :all, :assign_nested_attributes
|
19
|
+
|
20
|
+
# @private
|
21
|
+
def initialize(association)
|
22
|
+
@_her_association = association
|
23
|
+
end
|
24
|
+
|
25
|
+
def association
|
26
|
+
@_her_association
|
27
|
+
end
|
28
|
+
|
29
|
+
# @private
|
30
|
+
def method_missing(name, *args, &block)
|
31
|
+
if :object_id == name # avoid redefining object_id
|
32
|
+
return association.fetch.object_id
|
33
|
+
end
|
34
|
+
|
35
|
+
# create a proxy to the fetched object's method
|
36
|
+
metaclass = (class << self; self; end)
|
37
|
+
metaclass.install_proxy_methods 'association.fetch', name
|
38
|
+
|
39
|
+
# resend message to fetched object
|
40
|
+
__send__(name, *args, &block)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Associations
|
4
|
+
class BelongsToAssociation < Association
|
5
|
+
|
6
|
+
# @private
|
7
|
+
def self.attach(klass, name, opts)
|
8
|
+
opts = {
|
9
|
+
:class_name => name.to_s.classify,
|
10
|
+
:name => name,
|
11
|
+
:data_key => name,
|
12
|
+
:default => nil,
|
13
|
+
:foreign_key => "#{name}_id",
|
14
|
+
:path => "/#{name.to_s.pluralize}/:id"
|
15
|
+
}.merge(opts)
|
16
|
+
klass.associations[:belongs_to] << opts
|
17
|
+
|
18
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
19
|
+
def #{name}
|
20
|
+
cached_name = :"@_her_association_#{name}"
|
21
|
+
|
22
|
+
cached_data = (instance_variable_defined?(cached_name) && instance_variable_get(cached_name))
|
23
|
+
cached_data || instance_variable_set(cached_name, Her::Model::Associations::BelongsToAssociation.proxy(self, #{opts.inspect}))
|
24
|
+
end
|
25
|
+
RUBY
|
26
|
+
end
|
27
|
+
|
28
|
+
# @private
|
29
|
+
def self.parse(*args)
|
30
|
+
parse_single(*args)
|
31
|
+
end
|
32
|
+
|
33
|
+
# Initialize a new object
|
34
|
+
#
|
35
|
+
# @example
|
36
|
+
# class User
|
37
|
+
# include Her::Model
|
38
|
+
# belongs_to :organization
|
39
|
+
# end
|
40
|
+
#
|
41
|
+
# class Organization
|
42
|
+
# include Her::Model
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# user = User.find(1)
|
46
|
+
# new_organization = user.organization.build(:name => "Foo Inc.")
|
47
|
+
# new_organization # => #<Organization name="Foo Inc.">
|
48
|
+
def build(attributes = {})
|
49
|
+
@klass.build(attributes)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Create a new object, save it and associate it to the parent
|
53
|
+
#
|
54
|
+
# @example
|
55
|
+
# class User
|
56
|
+
# include Her::Model
|
57
|
+
# belongs_to :organization
|
58
|
+
# end
|
59
|
+
#
|
60
|
+
# class Organization
|
61
|
+
# include Her::Model
|
62
|
+
# end
|
63
|
+
#
|
64
|
+
# user = User.find(1)
|
65
|
+
# user.organization.create(:name => "Foo Inc.")
|
66
|
+
# user.organization # => #<Organization id=2 name="Foo Inc.">
|
67
|
+
def create(attributes = {})
|
68
|
+
resource = build(attributes)
|
69
|
+
@parent.attributes[@name] = resource if resource.save
|
70
|
+
resource
|
71
|
+
end
|
72
|
+
|
73
|
+
# @private
|
74
|
+
def fetch
|
75
|
+
foreign_key_value = @parent.attributes[@opts[:foreign_key].to_sym]
|
76
|
+
data_key_value = @parent.attributes[@opts[:data_key].to_sym]
|
77
|
+
return @opts[:default].try(:dup) if (@parent.attributes.include?(@name) && @parent.attributes[@name].nil? && @params.empty?) || (foreign_key_value.blank? && data_key_value.blank?)
|
78
|
+
|
79
|
+
return @cached_result unless @params.any? || @cached_result.nil?
|
80
|
+
return @parent.attributes[@name] unless @params.any? || @parent.attributes[@name].blank?
|
81
|
+
|
82
|
+
path_params = @parent.attributes.merge(@params.merge(@klass.primary_key => foreign_key_value))
|
83
|
+
path = build_association_path lambda { @klass.build_request_path(path_params) }
|
84
|
+
@klass.get_resource(path, @params).tap do |result|
|
85
|
+
@cached_result = result if @params.blank?
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
# @private
|
90
|
+
def assign_nested_attributes(attributes)
|
91
|
+
assign_single_nested_attributes(attributes)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Associations
|
4
|
+
class HasManyAssociation < Association
|
5
|
+
|
6
|
+
# @private
|
7
|
+
def self.attach(klass, name, opts)
|
8
|
+
opts = {
|
9
|
+
:class_name => name.to_s.classify,
|
10
|
+
:name => name,
|
11
|
+
:data_key => name,
|
12
|
+
:default => Her::Collection.new,
|
13
|
+
:path => "/#{name}",
|
14
|
+
:inverse_of => nil
|
15
|
+
}.merge(opts)
|
16
|
+
klass.associations[:has_many] << opts
|
17
|
+
|
18
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
19
|
+
def #{name}
|
20
|
+
cached_name = :"@_her_association_#{name}"
|
21
|
+
|
22
|
+
cached_data = (instance_variable_defined?(cached_name) && instance_variable_get(cached_name))
|
23
|
+
cached_data || instance_variable_set(cached_name, Her::Model::Associations::HasManyAssociation.proxy(self, #{opts.inspect}))
|
24
|
+
end
|
25
|
+
RUBY
|
26
|
+
end
|
27
|
+
|
28
|
+
# @private
|
29
|
+
def self.parse(association, klass, data)
|
30
|
+
data_key = association[:data_key]
|
31
|
+
return {} unless data[data_key]
|
32
|
+
|
33
|
+
klass = klass.her_nearby_class(association[:class_name])
|
34
|
+
{ association[:name] => Her::Model::Attributes.initialize_collection(klass, :data => data[data_key]) }
|
35
|
+
end
|
36
|
+
|
37
|
+
# Initialize a new object with a foreign key to the parent
|
38
|
+
#
|
39
|
+
# @example
|
40
|
+
# class User
|
41
|
+
# include Her::Model
|
42
|
+
# has_many :comments
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# class Comment
|
46
|
+
# include Her::Model
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# user = User.find(1)
|
50
|
+
# new_comment = user.comments.build(:body => "Hello!")
|
51
|
+
# new_comment # => #<Comment user_id=1 body="Hello!">
|
52
|
+
# TODO: This only merges the id of the parents, handle the case
|
53
|
+
# where this is more deeply nested
|
54
|
+
def build(attributes = {})
|
55
|
+
@klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id))
|
56
|
+
end
|
57
|
+
|
58
|
+
# Create a new object, save it and add it to the associated collection
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# class User
|
62
|
+
# include Her::Model
|
63
|
+
# has_many :comments
|
64
|
+
# end
|
65
|
+
#
|
66
|
+
# class Comment
|
67
|
+
# include Her::Model
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# user = User.find(1)
|
71
|
+
# user.comments.create(:body => "Hello!")
|
72
|
+
# user.comments # => [#<Comment id=2 user_id=1 body="Hello!">]
|
73
|
+
def create(attributes = {})
|
74
|
+
resource = build(attributes)
|
75
|
+
|
76
|
+
if resource.save
|
77
|
+
@parent.attributes[@name] ||= Her::Collection.new
|
78
|
+
@parent.attributes[@name] << resource
|
79
|
+
end
|
80
|
+
|
81
|
+
resource
|
82
|
+
end
|
83
|
+
|
84
|
+
# @private
|
85
|
+
def fetch
|
86
|
+
super.tap do |o|
|
87
|
+
inverse_of = @opts[:inverse_of] || @parent.singularized_resource_name
|
88
|
+
o.each { |entry| entry.send("#{inverse_of}=", @parent) }
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
# @private
|
93
|
+
def assign_nested_attributes(attributes)
|
94
|
+
data = attributes.is_a?(Hash) ? attributes.values : attributes
|
95
|
+
@parent.attributes[@name] = Her::Model::Attributes.initialize_collection(@klass, :data => data)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Associations
|
4
|
+
class HasOneAssociation < Association
|
5
|
+
|
6
|
+
# @private
|
7
|
+
def self.attach(klass, name, opts)
|
8
|
+
opts = {
|
9
|
+
:class_name => name.to_s.classify,
|
10
|
+
:name => name,
|
11
|
+
:data_key => name,
|
12
|
+
:default => nil,
|
13
|
+
:path => "/#{name}"
|
14
|
+
}.merge(opts)
|
15
|
+
klass.associations[:has_one] << opts
|
16
|
+
|
17
|
+
klass.class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
18
|
+
def #{name}
|
19
|
+
cached_name = :"@_her_association_#{name}"
|
20
|
+
|
21
|
+
cached_data = (instance_variable_defined?(cached_name) && instance_variable_get(cached_name))
|
22
|
+
cached_data || instance_variable_set(cached_name, Her::Model::Associations::HasOneAssociation.proxy(self, #{opts.inspect}))
|
23
|
+
end
|
24
|
+
RUBY
|
25
|
+
end
|
26
|
+
|
27
|
+
# @private
|
28
|
+
def self.parse(*args)
|
29
|
+
parse_single(*args)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Initialize a new object with a foreign key to the parent
|
33
|
+
#
|
34
|
+
# @example
|
35
|
+
# class User
|
36
|
+
# include Her::Model
|
37
|
+
# has_one :role
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
# class Role
|
41
|
+
# include Her::Model
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# user = User.find(1)
|
45
|
+
# new_role = user.role.build(:title => "moderator")
|
46
|
+
# new_role # => #<Role user_id=1 title="moderator">
|
47
|
+
def build(attributes = {})
|
48
|
+
@klass.build(attributes.merge(:"#{@parent.singularized_resource_name}_id" => @parent.id))
|
49
|
+
end
|
50
|
+
|
51
|
+
# Create a new object, save it and associate it to the parent
|
52
|
+
#
|
53
|
+
# @example
|
54
|
+
# class User
|
55
|
+
# include Her::Model
|
56
|
+
# has_one :role
|
57
|
+
# end
|
58
|
+
#
|
59
|
+
# class Role
|
60
|
+
# include Her::Model
|
61
|
+
# end
|
62
|
+
#
|
63
|
+
# user = User.find(1)
|
64
|
+
# user.role.create(:title => "moderator")
|
65
|
+
# user.role # => #<Role id=2 user_id=1 title="moderator">
|
66
|
+
def create(attributes = {})
|
67
|
+
resource = build(attributes)
|
68
|
+
@parent.attributes[@name] = resource if resource.save
|
69
|
+
resource
|
70
|
+
end
|
71
|
+
|
72
|
+
# @private
|
73
|
+
def assign_nested_attributes(attributes)
|
74
|
+
assign_single_nested_attributes(attributes)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,141 @@
|
|
1
|
+
require "her/model/associations/association"
|
2
|
+
require "her/model/associations/association_proxy"
|
3
|
+
require "her/model/associations/belongs_to_association"
|
4
|
+
require "her/model/associations/has_many_association"
|
5
|
+
require "her/model/associations/has_one_association"
|
6
|
+
|
7
|
+
module Her
|
8
|
+
module Model
|
9
|
+
# This module adds associations to models
|
10
|
+
module Associations
|
11
|
+
extend ActiveSupport::Concern
|
12
|
+
|
13
|
+
# Returns true if the model has a association_name association, false otherwise.
|
14
|
+
#
|
15
|
+
# @private
|
16
|
+
def has_association?(association_name)
|
17
|
+
associations = self.class.associations.values.flatten.map { |r| r[:name] }
|
18
|
+
associations.include?(association_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Returns the resource/collection corresponding to the association_name association.
|
22
|
+
#
|
23
|
+
# @private
|
24
|
+
def get_association(association_name)
|
25
|
+
send(association_name) if has_association?(association_name)
|
26
|
+
end
|
27
|
+
|
28
|
+
module ClassMethods
|
29
|
+
# Return @_her_associations, lazily initialized with copy of the
|
30
|
+
# superclass' her_associations, or an empty hash.
|
31
|
+
#
|
32
|
+
# @private
|
33
|
+
def associations
|
34
|
+
@_her_associations ||= begin
|
35
|
+
superclass.respond_to?(:associations) ? superclass.associations.dup : Hash.new { |h,k| h[k] = [] }
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
# @private
|
40
|
+
def association_names
|
41
|
+
associations.inject([]) { |memo, (name, details)| memo << details }.flatten.map { |a| a[:name] }
|
42
|
+
end
|
43
|
+
|
44
|
+
# @private
|
45
|
+
def association_keys
|
46
|
+
associations.inject([]) { |memo, (name, details)| memo << details }.flatten.map { |a| a[:data_key] }
|
47
|
+
end
|
48
|
+
|
49
|
+
# Parse associations data after initializing a new object
|
50
|
+
#
|
51
|
+
# @private
|
52
|
+
def parse_associations(data)
|
53
|
+
associations.each_pair do |type, definitions|
|
54
|
+
definitions.each do |association|
|
55
|
+
association_class = "her/model/associations/#{type}_association".classify.constantize
|
56
|
+
data.merge! association_class.parse(association, self, data)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
data
|
61
|
+
end
|
62
|
+
|
63
|
+
# Define an *has_many* association.
|
64
|
+
#
|
65
|
+
# @param [Symbol] name The name of the method added to resources
|
66
|
+
# @param [Hash] opts Options
|
67
|
+
# @option opts [String] :class_name The name of the class to map objects to
|
68
|
+
# @option opts [Symbol] :data_key The attribute where the data is stored
|
69
|
+
# @option opts [Path] :path The relative path where to fetch the data (defaults to `/{name}`)
|
70
|
+
#
|
71
|
+
# @example
|
72
|
+
# class User
|
73
|
+
# include Her::Model
|
74
|
+
# has_many :articles
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# class Article
|
78
|
+
# include Her::Model
|
79
|
+
# end
|
80
|
+
#
|
81
|
+
# @user = User.find(1)
|
82
|
+
# @user.articles # => [#<Article(articles/2) id=2 title="Hello world.">]
|
83
|
+
# # Fetched via GET "/users/1/articles"
|
84
|
+
def has_many(name, opts={})
|
85
|
+
Her::Model::Associations::HasManyAssociation.attach(self, name, opts)
|
86
|
+
end
|
87
|
+
|
88
|
+
# Define an *has_one* association.
|
89
|
+
#
|
90
|
+
# @param [Symbol] name The name of the method added to resources
|
91
|
+
# @param [Hash] opts Options
|
92
|
+
# @option opts [String] :class_name The name of the class to map objects to
|
93
|
+
# @option opts [Symbol] :data_key The attribute where the data is stored
|
94
|
+
# @option opts [Path] :path The relative path where to fetch the data (defaults to `/{name}`)
|
95
|
+
#
|
96
|
+
# @example
|
97
|
+
# class User
|
98
|
+
# include Her::Model
|
99
|
+
# has_one :organization
|
100
|
+
# end
|
101
|
+
#
|
102
|
+
# class Organization
|
103
|
+
# include Her::Model
|
104
|
+
# end
|
105
|
+
#
|
106
|
+
# @user = User.find(1)
|
107
|
+
# @user.organization # => #<Organization(organizations/2) id=2 name="Foobar Inc.">
|
108
|
+
# # Fetched via GET "/users/1/organization"
|
109
|
+
def has_one(name, opts={})
|
110
|
+
Her::Model::Associations::HasOneAssociation.attach(self, name, opts)
|
111
|
+
end
|
112
|
+
|
113
|
+
# Define a *belongs_to* association.
|
114
|
+
#
|
115
|
+
# @param [Symbol] name The name of the method added to resources
|
116
|
+
# @param [Hash] opts Options
|
117
|
+
# @option opts [String] :class_name The name of the class to map objects to
|
118
|
+
# @option opts [Symbol] :data_key The attribute where the data is stored
|
119
|
+
# @option opts [Path] :path The relative path where to fetch the data (defaults to `/{class_name}.pluralize/{id}`)
|
120
|
+
# @option opts [Symbol] :foreign_key The foreign key used to build the `:id` part of the path (defaults to `{name}_id`)
|
121
|
+
#
|
122
|
+
# @example
|
123
|
+
# class User
|
124
|
+
# include Her::Model
|
125
|
+
# belongs_to :team, :class_name => "Group"
|
126
|
+
# end
|
127
|
+
#
|
128
|
+
# class Group
|
129
|
+
# include Her::Model
|
130
|
+
# end
|
131
|
+
#
|
132
|
+
# @user = User.find(1) # => #<User(users/1) id=1 team_id=2 name="Tobias">
|
133
|
+
# @user.team # => #<Team(teams/2) id=2 name="Developers">
|
134
|
+
# # Fetched via GET "/teams/2"
|
135
|
+
def belongs_to(name, opts={})
|
136
|
+
Her::Model::Associations::BelongsToAssociation.attach(self, name, opts)
|
137
|
+
end
|
138
|
+
end
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|