Skip to content

Commit 9f58ec5

Browse files
committed
Merge pull request #308 from gschorkopf/access-levels
Prefer public_send to collaborating objects
2 parents 525ee5a + 489e6fd commit 9f58ec5

4 files changed

Lines changed: 20 additions & 20 deletions

File tree

lib/jsonapi/link_builder.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def build_engine_name
5858

5959
def engine_path_from_resource_class(klass)
6060
path_name = engine_resources_path_name_from_class(klass)
61-
engine_name.routes.url_helpers.send(path_name)
61+
engine_name.routes.url_helpers.public_send(path_name)
6262
end
6363

6464
def engine_primary_resources_path
@@ -71,7 +71,7 @@ def engine_primary_resources_url
7171

7272
def engine_resource_path(source)
7373
resource_path_name = engine_resource_path_name_from_source(source)
74-
engine_name.routes.url_helpers.send(resource_path_name, source.id)
74+
engine_name.routes.url_helpers.public_send(resource_path_name, source.id)
7575
end
7676

7777
def engine_resource_path_name_from_source(source)

lib/jsonapi/operation.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def initialize(resource_klass, options = {})
126126
def apply
127127
source_resource = @source_klass.find_by_key(@source_id, context: @context)
128128

129-
related_resource = source_resource.send(@association_type)
129+
related_resource = source_resource.public_send(@association_type)
130130

131131
return JSONAPI::ResourceOperationResult.new(:ok, related_resource)
132132

@@ -152,7 +152,7 @@ def initialize(resource_klass, options = {})
152152
def apply
153153
source_resource = @source_klass.find_by_key(@source_id, context: @context)
154154

155-
related_resource = source_resource.send(@association_type,
155+
related_resource = source_resource.public_send(@association_type,
156156
filters: @filters,
157157
sort_criteria: @sort_criteria,
158158
paginator: @paginator)

lib/jsonapi/resource.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def initialize(model, context = nil)
2828
end
2929

3030
def id
31-
model.send(self.class._primary_key)
31+
model.public_send(self.class._primary_key)
3232
end
3333

3434
def is_new?
@@ -112,7 +112,7 @@ def fetchable_fields
112112
# Override this on a resource to customize how the associated records
113113
# are fetched for a model. Particularly helpful for authorization.
114114
def records_for(association_name, _options = {})
115-
model.send association_name
115+
model.public_send association_name
116116
end
117117

118118
private
@@ -166,9 +166,9 @@ def _create_has_many_links(association_type, association_key_values)
166166
related_resource = association.resource_klass.find_by_key(association_key_value, context: @context)
167167

168168
# TODO: Add option to skip relations that already exist instead of returning an error?
169-
relation = @model.send(association.type).where(association.primary_key => association_key_value).first
169+
relation = @model.public_send(association.type).where(association.primary_key => association_key_value).first
170170
if relation.nil?
171-
@model.send(association.type) << related_resource.model
171+
@model.public_send(association.type) << related_resource.model
172172
else
173173
fail JSONAPI::Exceptions::HasManyRelationExists.new(association_key_value)
174174
end
@@ -198,8 +198,8 @@ def _replace_has_one_link(association_type, association_key_value)
198198
def _replace_polymorphic_has_one_link(association_type, key_value, key_type)
199199
association = self.class._associations[association_type.to_sym]
200200

201-
model.send("#{association.foreign_key}=", key_value)
202-
model.send("#{association.polymorphic_type}=", key_type.to_s.classify)
201+
model.public_send("#{association.foreign_key}=", key_value)
202+
model.public_send("#{association.polymorphic_type}=", key_type.to_s.classify)
203203

204204
@save_needed = true
205205

@@ -209,7 +209,7 @@ def _replace_polymorphic_has_one_link(association_type, key_value, key_type)
209209
def _remove_has_many_link(association_type, key)
210210
association = self.class._associations[association_type]
211211

212-
@model.send(association.type).delete(key)
212+
@model.public_send(association.type).delete(key)
213213

214214
:completed
215215
end
@@ -314,11 +314,11 @@ def attribute(attr, options = {})
314314
@_attributes ||= {}
315315
@_attributes[attr] = options
316316
define_method attr do
317-
@model.send(attr)
317+
@model.public_send(attr)
318318
end unless method_defined?(attr)
319319

320320
define_method "#{attr}=" do |value|
321-
@model.send "#{attr}=", value
321+
@model.public_send "#{attr}=", value
322322
end unless method_defined?("#{attr}=")
323323
end
324324

@@ -696,7 +696,7 @@ def _associate(klass, *attrs)
696696
define_method foreign_key do
697697
records = public_send(associated_records_method_name)
698698
return records.collect do |record|
699-
record.send(association.resource_klass._primary_key)
699+
record.public_send(association.resource_klass._primary_key)
700700
end
701701
end unless method_defined?(foreign_key)
702702
define_method attr do |options = {}|

lib/jsonapi/resource_serializer.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ def attribute_hash(source)
132132
fields.each_with_object({}) do |name, hash|
133133
format = source.class._attribute_options(name)[:format]
134134
unless name == :id
135-
hash[format_key(name)] = format_value(source.send(name), format)
135+
hash[format_key(name)] = format_value(source.public_send(name), format)
136136
end
137137
end
138138
end
@@ -167,7 +167,7 @@ def relationship_data(source, include_directives)
167167
# through the associations.
168168
if include_linkage || include_linked_children
169169
if association.is_a?(JSONAPI::Association::HasOne)
170-
resource = source.send(name)
170+
resource = source.public_send(name)
171171
if resource
172172
id = resource.id
173173
type = association.type_for_source(source)
@@ -179,7 +179,7 @@ def relationship_data(source, include_directives)
179179
end
180180
end
181181
elsif association.is_a?(JSONAPI::Association::HasMany)
182-
resources = source.send(name)
182+
resources = source.public_send(name)
183183
resources.each do |resource|
184184
id = resource.id
185185
associations_only = already_serialized?(type, id)
@@ -267,18 +267,18 @@ def link_object(source, association, include_linkage = false)
267267
# Extracts the foreign key value for a has_one association.
268268
def foreign_key_value(source, association)
269269
foreign_key = association.foreign_key
270-
value = source.send(foreign_key)
270+
value = source.public_send(foreign_key)
271271
IdValueFormatter.format(value)
272272
end
273273

274274
def foreign_key_types_and_values(source, association)
275275
if association.is_a?(JSONAPI::Association::HasMany)
276276
if association.polymorphic?
277-
source.model.send(association.name).pluck(:type, :id).map do |type, id|
277+
source.model.public_send(association.name).pluck(:type, :id).map do |type, id|
278278
[type.pluralize, IdValueFormatter.format(id)]
279279
end
280280
else
281-
source.send(association.foreign_key).map do |value|
281+
source.public_send(association.foreign_key).map do |value|
282282
[association.type, IdValueFormatter.format(value)]
283283
end
284284
end

0 commit comments

Comments
 (0)