Skip to content

Commit

Permalink
Reduce internal API changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pablobm committed Jul 9, 2020
1 parent e27224e commit dd87b6c
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 28 deletions.
5 changes: 4 additions & 1 deletion lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ def form_attributes

def permitted_attributes
form_attributes.map do |attr|
attribute_types[attr].permitted_attribute(self.class.model, attr)
attribute_types[attr].permitted_attribute(
attr,
resource_class: self.class.model,
)
end.uniq
end

Expand Down
2 changes: 1 addition & 1 deletion lib/administrate/field/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def self.field_type
to_s.split("::").last.underscore
end

def self.permitted_attribute(_resource_class, attr)
def self.permitted_attribute(attr, _options = nil)
attr
end

Expand Down
10 changes: 8 additions & 2 deletions lib/administrate/field/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
module Administrate
module Field
class BelongsTo < Associative
def self.permitted_attribute(resource_class, attr)
foreign_key_for(resource_class, attr)
def self.permitted_attribute(attr, options = {})
resource_class = options[:resource_class]
if resource_class
foreign_key_for(resource_class, attr)
else
# TODO: deprecate
:"#{attr}_id"
end
end

def permitted_attribute
Expand Down
4 changes: 2 additions & 2 deletions lib/administrate/field/deferred.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ def searchable_fields
end
end

def permitted_attribute(resource_class, attr)
def permitted_attribute(attr, opts = {})
# TODO: deprecate :foreign_key option
options.fetch(:foreign_key) do
deferred_class.permitted_attribute(resource_class, attr)
deferred_class.permitted_attribute(attr, opts)
end
end

Expand Down
7 changes: 5 additions & 2 deletions lib/administrate/field/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Field
class HasMany < Associative
DEFAULT_LIMIT = 5

def self.permitted_attribute(_resource_class, attr)
def self.permitted_attribute(attr, _options = {})
{ "#{attr.to_s.singularize}_ids".to_sym => [] }
end

Expand Down Expand Up @@ -36,7 +36,10 @@ def limit
end

def permitted_attribute
self.class.permitted_attribute(resource, attribute)
self.class.permitted_attribute(
attribute,
resource_class: resource.class,
)
end

def resources(page = 1, order = self.order)
Expand Down
29 changes: 15 additions & 14 deletions lib/administrate/field/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,22 @@
module Administrate
module Field
class HasOne < Associative
def self.permitted_attribute(resource_class, attr, deprecated_options: {})
resource_path = deprecated_associated_class_name(
resource_class,
attr,
deprecated_options: deprecated_options,
)
def self.permitted_attribute(attr, options = {})
resource_class = options[:resource_class]
_associated_class_name =
if resource_class
associated_class_name(resource_class, attr)
else
# TODO: deprecate
if options
options.fetch(:class_name, attr.to_s.singularize.camelcase)
else
attr
end
end
related_dashboard_attributes =
Administrate::ResourceResolver.new("admin/#{resource_path}").
Administrate::ResourceResolver.new("admin/#{_associated_class_name}").
dashboard_class.new.permitted_attributes + [:id]

{ "#{attr}_attributes": related_dashboard_attributes }
end

Expand All @@ -38,13 +44,8 @@ def nested_form
private

def resolver
resource_path = self.class.deprecated_associated_class_name(
resource.class,
attribute,
deprecated_options: options,
)
@resolver ||=
Administrate::ResourceResolver.new("admin/#{resource_path}")
Administrate::ResourceResolver.new("admin/#{associated_class.name}")
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/administrate/field/polymorphic.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module Administrate
module Field
class Polymorphic < BelongsTo
def self.permitted_attribute(_resource_class, attr)
def self.permitted_attribute(attr, _options = {})
{ attr => %i{type value} }
end

Expand Down
6 changes: 3 additions & 3 deletions spec/lib/fields/deferred_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Administrate::Field::BelongsTo,
foreign_key: :bar,
)
expect(deferred.permitted_attribute(LineItem, :foo)).to eq(:bar)
expect(deferred.permitted_attribute(:foo, resource_class: LineItem)).to eq(:bar)
end
end

Expand All @@ -22,10 +22,10 @@
)
allow(Administrate::Field::String).to receive(:permitted_attribute)

deferred.permitted_attribute(LineItem, :foo)
deferred.permitted_attribute(:foo, resource_class: LineItem)

expect(Administrate::Field::String).
to have_received(:permitted_attribute).with(LineItem, :foo)
to have_received(:permitted_attribute).with(:foo, resource_class: LineItem)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/fields/has_one_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
)

field_name = "product_meta_tag"
attributes = field.permitted_attribute(Product, field_name)
attributes = field.permitted_attribute(field_name, resource_class: Product)
expect(attributes[:"#{field_name}_attributes"]).
to eq(%i(meta_title meta_description id))
end
Expand Down
5 changes: 4 additions & 1 deletion spec/support/field_matchers.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module FieldMatchers
def should_permit_param(expected_param, for_attribute:, on_model:)
permitted_param = described_class.permitted_attribute(on_model, for_attribute)
permitted_param = described_class.permitted_attribute(
for_attribute,
resource_class: on_model,
)
expect(permitted_param).to eq(expected_param)
end
end

0 comments on commit dd87b6c

Please sign in to comment.