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,304 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
# This module handles all methods related to model attributes
|
4
|
+
module Attributes
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
ATTRIBUTE_BLACKLIST = [:attribute_changed_in_place?].to_set
|
8
|
+
|
9
|
+
# Initialize a new object with data
|
10
|
+
#
|
11
|
+
# @param [Hash] attributes The attributes to initialize the object with
|
12
|
+
# @option attributes [Hash,Array] :_metadata
|
13
|
+
# @option attributes [Hash,Array] :_errors
|
14
|
+
# @option attributes [Boolean] :_destroyed
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# class User
|
18
|
+
# include Her::Model
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# User.new(name: "Tobias") # => #<User name="Tobias">
|
22
|
+
def initialize(attributes={})
|
23
|
+
attributes ||= {}
|
24
|
+
@metadata = attributes.delete(:_metadata) || {}
|
25
|
+
@response_errors = attributes.delete(:_errors) || {}
|
26
|
+
@destroyed = attributes.delete(:_destroyed) || false
|
27
|
+
|
28
|
+
attributes = self.class.default_scope.apply_to(attributes)
|
29
|
+
assign_attributes(attributes)
|
30
|
+
run_callbacks :initialize
|
31
|
+
end
|
32
|
+
|
33
|
+
# Initialize a collection of resources
|
34
|
+
#
|
35
|
+
# @private
|
36
|
+
def self.initialize_collection(klass, parsed_data={})
|
37
|
+
unless parsed_data[:errors].present?
|
38
|
+
initialize_resource_collection(klass, parsed_data)
|
39
|
+
else
|
40
|
+
initialize_error_collection(parsed_data)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.initialize_resource_collection(klass, parsed_data={})
|
45
|
+
collection_data = klass.extract_array(parsed_data).map do |item_data|
|
46
|
+
if item_data.kind_of?(klass)
|
47
|
+
resource = item_data
|
48
|
+
else
|
49
|
+
resource = klass.new(klass.parse(item_data))
|
50
|
+
resource.instance_variable_set(:@changed_attributes, {})
|
51
|
+
resource.run_callbacks :find
|
52
|
+
end
|
53
|
+
resource
|
54
|
+
end
|
55
|
+
Her::Collection.new(collection_data, parsed_data[:metadata], parsed_data[:errors])
|
56
|
+
end
|
57
|
+
|
58
|
+
# Initialize an error collection
|
59
|
+
#
|
60
|
+
# @private
|
61
|
+
def self.initialize_error_collection(parsed_data={})
|
62
|
+
Her::ErrorCollection.new(parsed_data[:metadata], parsed_data[:errors])
|
63
|
+
end
|
64
|
+
|
65
|
+
# Use setter methods of model for each key / value pair in params
|
66
|
+
# Return key / value pairs for which no setter method was defined on the model
|
67
|
+
#
|
68
|
+
# @private
|
69
|
+
def self.use_setter_methods(model, params)
|
70
|
+
params ||= {}
|
71
|
+
|
72
|
+
reserved_keys = [:id, model.class.primary_key] + model.class.association_keys
|
73
|
+
model.class.attributes *params.keys.reject { |k| reserved_keys.include?(k) || reserved_keys.map(&:to_s).include?(k) }
|
74
|
+
|
75
|
+
setter_method_names = model.class.setter_method_names
|
76
|
+
params.inject({}) do |memo, (key, value)|
|
77
|
+
setter_method = key.to_s + '='
|
78
|
+
if setter_method_names.include?(setter_method)
|
79
|
+
model.send(setter_method, value)
|
80
|
+
else
|
81
|
+
key = key.to_sym if key.is_a?(String)
|
82
|
+
memo[key] = value
|
83
|
+
end
|
84
|
+
memo
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
# Handles missing methods
|
89
|
+
#
|
90
|
+
# @private
|
91
|
+
def method_missing(method, *args, &blk)
|
92
|
+
return super if ATTRIBUTE_BLACKLIST.include?(method)
|
93
|
+
if method.to_s =~ /[?=]$/ || @attributes.include?(method)
|
94
|
+
# Extract the attribute
|
95
|
+
attribute = method.to_s.sub(/[?=]$/, '')
|
96
|
+
|
97
|
+
# Create a new `attribute` methods set
|
98
|
+
self.class.attributes(*attribute)
|
99
|
+
|
100
|
+
# Resend the method!
|
101
|
+
send(method, *args, &blk)
|
102
|
+
else
|
103
|
+
super
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
# @private
|
108
|
+
def respond_to_missing?(method, include_private = false)
|
109
|
+
return super if ATTRIBUTE_BLACKLIST.include?(method)
|
110
|
+
method.to_s.end_with?('=') || method.to_s.end_with?('?') || attributes.include?(method) || super
|
111
|
+
end
|
112
|
+
|
113
|
+
# Assign new attributes to a resource
|
114
|
+
#
|
115
|
+
# @example
|
116
|
+
# class User
|
117
|
+
# include Her::Model
|
118
|
+
# end
|
119
|
+
#
|
120
|
+
# user = User.find(1) # => #<User id=1 name="Tobias">
|
121
|
+
# user.assign_attributes(name: "Lindsay")
|
122
|
+
# user.changes # => { :name => ["Tobias", "Lindsay"] }
|
123
|
+
def assign_attributes(new_attributes)
|
124
|
+
@attributes ||= attributes
|
125
|
+
# Use setter methods first
|
126
|
+
unset_attributes = Her::Model::Attributes.use_setter_methods(self, new_attributes)
|
127
|
+
|
128
|
+
# Then translate attributes of associations into association instances
|
129
|
+
parsed_attributes = self.class.parse_associations(unset_attributes)
|
130
|
+
|
131
|
+
# Then merge the parsed_data into @attributes.
|
132
|
+
@attributes.merge!(parsed_attributes)
|
133
|
+
end
|
134
|
+
alias attributes= assign_attributes
|
135
|
+
|
136
|
+
def attributes
|
137
|
+
@attributes ||= HashWithIndifferentAccess.new
|
138
|
+
end
|
139
|
+
|
140
|
+
# Handles returning true for the accessible attributes
|
141
|
+
#
|
142
|
+
# @private
|
143
|
+
def has_attribute?(attribute_name)
|
144
|
+
@attributes.include?(attribute_name)
|
145
|
+
end
|
146
|
+
|
147
|
+
# Handles returning data for a specific attribute
|
148
|
+
#
|
149
|
+
# @private
|
150
|
+
def get_attribute(attribute_name)
|
151
|
+
@attributes[attribute_name]
|
152
|
+
end
|
153
|
+
alias attribute get_attribute
|
154
|
+
|
155
|
+
# Return the value of the model `primary_key` attribute
|
156
|
+
def id
|
157
|
+
@attributes[self.class.primary_key]
|
158
|
+
end
|
159
|
+
|
160
|
+
# Return `true` if the other object is also a Her::Model and has matching data
|
161
|
+
#
|
162
|
+
# @private
|
163
|
+
def ==(other)
|
164
|
+
other.is_a?(Her::Model) && @attributes == other.attributes
|
165
|
+
end
|
166
|
+
|
167
|
+
# Delegate to the == method
|
168
|
+
#
|
169
|
+
# @private
|
170
|
+
def eql?(other)
|
171
|
+
self == other
|
172
|
+
end
|
173
|
+
|
174
|
+
# Delegate to @attributes, allowing models to act correctly in code like:
|
175
|
+
# [ Model.find(1), Model.find(1) ].uniq # => [ Model.find(1) ]
|
176
|
+
# @private
|
177
|
+
def hash
|
178
|
+
@attributes.hash
|
179
|
+
end
|
180
|
+
|
181
|
+
# Assign attribute value (ActiveModel convention method).
|
182
|
+
#
|
183
|
+
# @private
|
184
|
+
def attribute=(attribute, value)
|
185
|
+
@attributes[attribute] = nil unless @attributes.include?(attribute)
|
186
|
+
self.send(:"#{attribute}_will_change!") if @attributes[attribute] != value
|
187
|
+
@attributes[attribute] = value
|
188
|
+
end
|
189
|
+
|
190
|
+
# Check attribute value to be present (ActiveModel convention method).
|
191
|
+
#
|
192
|
+
# @private
|
193
|
+
def attribute?(attribute)
|
194
|
+
@attributes.include?(attribute) && @attributes[attribute].present?
|
195
|
+
end
|
196
|
+
|
197
|
+
module ClassMethods
|
198
|
+
# Initialize a collection of resources with raw data from an HTTP request
|
199
|
+
#
|
200
|
+
# @param [Array] parsed_data
|
201
|
+
# @private
|
202
|
+
def new_collection(parsed_data)
|
203
|
+
Her::Model::Attributes.initialize_collection(self, parsed_data)
|
204
|
+
end
|
205
|
+
|
206
|
+
# Initialize a new object with the "raw" parsed_data from the parsing middleware
|
207
|
+
#
|
208
|
+
# @private
|
209
|
+
def new_from_parsed_data(parsed_data)
|
210
|
+
parsed_data = parsed_data.with_indifferent_access
|
211
|
+
new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:metadata], :_errors => parsed_data[:errors])
|
212
|
+
end
|
213
|
+
|
214
|
+
# Define attribute method matchers to automatically define them using ActiveModel's define_attribute_methods.
|
215
|
+
#
|
216
|
+
# @private
|
217
|
+
def define_attribute_method_matchers
|
218
|
+
attribute_method_suffix '='
|
219
|
+
attribute_method_suffix '?'
|
220
|
+
end
|
221
|
+
|
222
|
+
# Create a mutex for dynamically generated attribute methods or use one defined by ActiveModel.
|
223
|
+
#
|
224
|
+
# @private
|
225
|
+
def attribute_methods_mutex
|
226
|
+
@attribute_methods_mutex ||= if generated_attribute_methods.respond_to? :mu_synchronize
|
227
|
+
generated_attribute_methods
|
228
|
+
else
|
229
|
+
Mutex.new
|
230
|
+
end
|
231
|
+
end
|
232
|
+
|
233
|
+
# Define the attributes that will be used to track dirty attributes and validations
|
234
|
+
#
|
235
|
+
# @param [Array] attributes
|
236
|
+
# @example
|
237
|
+
# class User
|
238
|
+
# include Her::Model
|
239
|
+
# attributes :name, :email
|
240
|
+
# end
|
241
|
+
def attributes(*attributes)
|
242
|
+
attribute_methods_mutex.synchronize do
|
243
|
+
define_attribute_methods attributes
|
244
|
+
end
|
245
|
+
end
|
246
|
+
|
247
|
+
# Define the accessor in which the API response errors (obtained from the parsing middleware) will be stored
|
248
|
+
#
|
249
|
+
# @param [Symbol] store_response_errors
|
250
|
+
#
|
251
|
+
# @example
|
252
|
+
# class User
|
253
|
+
# include Her::Model
|
254
|
+
# store_response_errors :server_errors
|
255
|
+
# end
|
256
|
+
def store_response_errors(value = nil)
|
257
|
+
store_her_data(:response_errors, value)
|
258
|
+
end
|
259
|
+
|
260
|
+
# Define the accessor in which the API response metadata (obtained from the parsing middleware) will be stored
|
261
|
+
#
|
262
|
+
# @param [Symbol] store_metadata
|
263
|
+
#
|
264
|
+
# @example
|
265
|
+
# class User
|
266
|
+
# include Her::Model
|
267
|
+
# store_metadata :server_data
|
268
|
+
# end
|
269
|
+
def store_metadata(value = nil)
|
270
|
+
store_her_data(:metadata, value)
|
271
|
+
end
|
272
|
+
|
273
|
+
# @private
|
274
|
+
def setter_method_names
|
275
|
+
@_her_setter_method_names ||= instance_methods.inject(Set.new) do |memo, method_name|
|
276
|
+
memo << method_name.to_s if method_name.to_s.end_with?('=')
|
277
|
+
memo
|
278
|
+
end
|
279
|
+
end
|
280
|
+
|
281
|
+
private
|
282
|
+
# @private
|
283
|
+
def store_her_data(name, value)
|
284
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
285
|
+
if @_her_store_#{name} && value.present?
|
286
|
+
remove_method @_her_store_#{name}.to_sym
|
287
|
+
remove_method @_her_store_#{name}.to_s + '='
|
288
|
+
end
|
289
|
+
|
290
|
+
@_her_store_#{name} ||= begin
|
291
|
+
superclass.store_#{name} if superclass.respond_to?(:store_#{name})
|
292
|
+
end
|
293
|
+
|
294
|
+
return @_her_store_#{name} unless value
|
295
|
+
@_her_store_#{name} = value
|
296
|
+
|
297
|
+
define_method(value) { @#{name} }
|
298
|
+
define_method(value.to_s+'=') { |value| @#{name} = value }
|
299
|
+
RUBY
|
300
|
+
end
|
301
|
+
end
|
302
|
+
end
|
303
|
+
end
|
304
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
# This module includes basic functionnality to Her::Model
|
4
|
+
module Base
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
# Returns true if attribute_name is
|
8
|
+
# * in resource attributes
|
9
|
+
# * an association
|
10
|
+
#
|
11
|
+
# @private
|
12
|
+
def has_key?(attribute_name)
|
13
|
+
has_attribute?(attribute_name) ||
|
14
|
+
has_association?(attribute_name)
|
15
|
+
end
|
16
|
+
|
17
|
+
# Returns
|
18
|
+
# * the value of the attribute_name attribute if it's in orm data
|
19
|
+
# * the resource/collection corrsponding to attribute_name if it's an association
|
20
|
+
#
|
21
|
+
# @private
|
22
|
+
def [](attribute_name)
|
23
|
+
get_attribute(attribute_name) ||
|
24
|
+
get_association(attribute_name)
|
25
|
+
end
|
26
|
+
|
27
|
+
# @private
|
28
|
+
def singularized_resource_name
|
29
|
+
self.class.name.split('::').last.tableize.singularize
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
# @private
|
4
|
+
module DeprecatedMethods
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def self.deprecate!(old, new, object, *args)
|
8
|
+
line = begin
|
9
|
+
raise StandardError
|
10
|
+
rescue StandardError => e
|
11
|
+
e.backtrace[2]
|
12
|
+
end
|
13
|
+
|
14
|
+
warn "#{line} - The `#{old}` method is deprecated and may be removed soon. Please update your code with `#{new}` instead."
|
15
|
+
object.send(new, *args)
|
16
|
+
end
|
17
|
+
|
18
|
+
def data(*args)
|
19
|
+
Her::Model::DeprecatedMethods.deprecate! :data, :attributes, self, *args
|
20
|
+
end
|
21
|
+
|
22
|
+
def data=(*args)
|
23
|
+
Her::Model::DeprecatedMethods.deprecate! :data=, :attributes=, self, *args
|
24
|
+
end
|
25
|
+
|
26
|
+
def update_attributes(*args)
|
27
|
+
Her::Model::DeprecatedMethods.deprecate! :update_attributes, :assign_attributes, self, *args
|
28
|
+
end
|
29
|
+
|
30
|
+
def assign_data(*args)
|
31
|
+
Her::Model::DeprecatedMethods.deprecate! :assign_data, :assign_attributes, self, *args
|
32
|
+
end
|
33
|
+
|
34
|
+
def has_data?(*args)
|
35
|
+
Her::Model::DeprecatedMethods.deprecate! :has_data?, :has_attribute?, self, *args
|
36
|
+
end
|
37
|
+
|
38
|
+
def get_data(*args)
|
39
|
+
Her::Model::DeprecatedMethods.deprecate! :get_data, :get_attribute, self, *args
|
40
|
+
end
|
41
|
+
|
42
|
+
module ClassMethods
|
43
|
+
def has_relationship?(*args)
|
44
|
+
Her::Model::DeprecatedMethods.deprecate! :has_relationship?, :has_association?, self, *args
|
45
|
+
end
|
46
|
+
|
47
|
+
def get_relationship(*args)
|
48
|
+
Her::Model::DeprecatedMethods.deprecate! :get_relationship, :get_association, self, *args
|
49
|
+
end
|
50
|
+
|
51
|
+
def relationships(*args)
|
52
|
+
Her::Model::DeprecatedMethods.deprecate! :relationships, :associations, self, *args
|
53
|
+
end
|
54
|
+
|
55
|
+
def her_api(*args)
|
56
|
+
Her::Model::DeprecatedMethods.deprecate! :her_api, :use_api, self, *args
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
# This module interacts with Her::API to fetch HTTP data
|
4
|
+
module HTTP
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
METHODS = [:get, :post, :put, :patch, :delete]
|
7
|
+
|
8
|
+
# For each HTTP method, define these class methods:
|
9
|
+
#
|
10
|
+
# - <method>(path, params)
|
11
|
+
# - <method>_raw(path, params, &block)
|
12
|
+
# - <method>_collection(path, params, &block)
|
13
|
+
# - <method>_resource(path, params, &block)
|
14
|
+
# - custom_<method>(*paths)
|
15
|
+
#
|
16
|
+
# @example
|
17
|
+
# class User
|
18
|
+
# include Her::Model
|
19
|
+
# custom_get :active
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# User.get(:popular) # GET "/users/popular"
|
23
|
+
# User.active # GET "/users/active"
|
24
|
+
module ClassMethods
|
25
|
+
# Change which API the model will use to make its HTTP requests
|
26
|
+
#
|
27
|
+
# @example
|
28
|
+
# secondary_api = Her::API.new :url => "https://api.example" do |connection|
|
29
|
+
# connection.use Faraday::Request::UrlEncoded
|
30
|
+
# connection.use Her::Middleware::DefaultParseJSON
|
31
|
+
# end
|
32
|
+
#
|
33
|
+
# class User
|
34
|
+
# include Her::Model
|
35
|
+
# use_api secondary_api
|
36
|
+
# end
|
37
|
+
def use_api(value = nil)
|
38
|
+
@_her_use_api ||= begin
|
39
|
+
superclass.use_api if superclass.respond_to?(:use_api)
|
40
|
+
end
|
41
|
+
|
42
|
+
unless value
|
43
|
+
return (@_her_use_api.respond_to? :call) ? @_her_use_api.call : @_her_use_api
|
44
|
+
end
|
45
|
+
|
46
|
+
@_her_use_api = value
|
47
|
+
end
|
48
|
+
|
49
|
+
alias her_api use_api
|
50
|
+
alias uses_api use_api
|
51
|
+
|
52
|
+
# Main request wrapper around Her::API. Used to make custom request to the API.
|
53
|
+
#
|
54
|
+
# @private
|
55
|
+
def request(params={})
|
56
|
+
request = her_api.request(params)
|
57
|
+
status = request[:response].status
|
58
|
+
|
59
|
+
raise Her::Errors::RemoteServerError.new("Request against #{self.name} returned a #{status}") if status >= 500
|
60
|
+
|
61
|
+
if block_given?
|
62
|
+
yield request[:parsed_data], request[:response]
|
63
|
+
else
|
64
|
+
{ :parsed_data => request[:parsed_data], :response => request[:response] }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
METHODS.each do |method|
|
69
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
70
|
+
def #{method}(path, params={})
|
71
|
+
path = build_request_path_from_string_or_symbol(path, params)
|
72
|
+
params = to_params(params) unless #{method.to_sym.inspect} == :get
|
73
|
+
send(:'#{method}_raw', path, params) do |parsed_data, response|
|
74
|
+
if parsed_data[:data].is_a?(Array) || active_model_serializers_format? || json_api_format?
|
75
|
+
new_collection(parsed_data)
|
76
|
+
else
|
77
|
+
new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:metadata], :_errors => parsed_data[:errors])
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def #{method}_raw(path, params={}, &block)
|
83
|
+
path = build_request_path_from_string_or_symbol(path, params)
|
84
|
+
request(params.merge(:_method => #{method.to_sym.inspect}, :_path => path), &block)
|
85
|
+
end
|
86
|
+
|
87
|
+
def #{method}_collection(path, params={})
|
88
|
+
path = build_request_path_from_string_or_symbol(path, params)
|
89
|
+
send(:'#{method}_raw', build_request_path_from_string_or_symbol(path, params), params) do |parsed_data, response|
|
90
|
+
new_collection(parsed_data)
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def #{method}_resource(path, params={})
|
95
|
+
path = build_request_path_from_string_or_symbol(path, params)
|
96
|
+
send(:"#{method}_raw", path, params) do |parsed_data, response|
|
97
|
+
new(parse(parsed_data[:data]).merge :_metadata => parsed_data[:metadata], :_errors => parsed_data[:errors])
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def custom_#{method}(*paths)
|
102
|
+
metaclass = (class << self; self; end)
|
103
|
+
opts = paths.last.is_a?(Hash) ? paths.pop : Hash.new
|
104
|
+
|
105
|
+
paths.each do |path|
|
106
|
+
metaclass.send(:define_method, path) do |*params|
|
107
|
+
params = params.first || Hash.new
|
108
|
+
send(#{method.to_sym.inspect}, path, params)
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
RUBY
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module Introspection
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
# Inspect an element, returns it for introspection.
|
6
|
+
#
|
7
|
+
# @example
|
8
|
+
# class User
|
9
|
+
# include Her::Model
|
10
|
+
# end
|
11
|
+
#
|
12
|
+
# @user = User.find(1)
|
13
|
+
# p @user # => #<User(/users/1) id=1 name="Tobias Fünke">
|
14
|
+
def inspect
|
15
|
+
resource_path = begin
|
16
|
+
request_path
|
17
|
+
rescue Her::Errors::PathError => e
|
18
|
+
"<unknown path, missing `#{e.missing_parameter}`>"
|
19
|
+
end
|
20
|
+
|
21
|
+
"#<#{self.class}(#{resource_path}) #{attributes.keys.map { |k| "#{k}=#{attribute_for_inspect(send(k))}" }.join(" ")}>"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
def attribute_for_inspect(value)
|
26
|
+
if value.is_a?(String) && value.length > 50
|
27
|
+
"#{value[0..50]}...".inspect
|
28
|
+
elsif value.is_a?(Date) || value.is_a?(Time)
|
29
|
+
%("#{value}")
|
30
|
+
else
|
31
|
+
value.inspect
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# @private
|
36
|
+
module ClassMethods
|
37
|
+
# Finds a class at the same level as this one or at the global level.
|
38
|
+
#
|
39
|
+
# @private
|
40
|
+
def her_nearby_class(name)
|
41
|
+
her_sibling_class(name) || name.constantize rescue nil
|
42
|
+
end
|
43
|
+
|
44
|
+
protected
|
45
|
+
# Looks for a class at the same level as this one with the given name.
|
46
|
+
#
|
47
|
+
# @private
|
48
|
+
def her_sibling_class(name)
|
49
|
+
if mod = self.her_containing_module
|
50
|
+
@_her_sibling_class ||= Hash.new { Hash.new }
|
51
|
+
@_her_sibling_class[mod][name] ||= "#{mod.name}::#{name}".constantize rescue nil
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# If available, returns the containing Module for this class.
|
56
|
+
#
|
57
|
+
# @private
|
58
|
+
def her_containing_module
|
59
|
+
return unless self.name =~ /::/
|
60
|
+
self.name.split("::")[0..-2].join("::").constantize
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Her
|
2
|
+
module Model
|
3
|
+
module NestedAttributes
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
# Allow nested attributes for an association
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
# class User
|
11
|
+
# include Her::Model
|
12
|
+
#
|
13
|
+
# has_one :role
|
14
|
+
# accepts_nested_attributes_for :role
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# class Role
|
18
|
+
# include Her::Model
|
19
|
+
# end
|
20
|
+
#
|
21
|
+
# user = User.new(name: "Tobias", role_attributes: { title: "moderator" })
|
22
|
+
# user.role # => #<Role title="moderator">
|
23
|
+
def accepts_nested_attributes_for(*associations)
|
24
|
+
allowed_association_names = association_names
|
25
|
+
|
26
|
+
associations.each do |association_name|
|
27
|
+
unless allowed_association_names.include?(association_name)
|
28
|
+
raise Her::Errors::AssociationUnknownError.new("Unknown association name :#{association_name}")
|
29
|
+
end
|
30
|
+
|
31
|
+
class_eval <<-RUBY, __FILE__, __LINE__ + 1
|
32
|
+
if method_defined?(:#{association_name}_attributes=)
|
33
|
+
remove_method(:#{association_name}_attributes=)
|
34
|
+
end
|
35
|
+
|
36
|
+
def #{association_name}_attributes=(attributes)
|
37
|
+
self.#{association_name}.assign_nested_attributes(attributes)
|
38
|
+
end
|
39
|
+
RUBY
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|