Skip to content

Commit

Permalink
Allow resource to be accessed from field (#788)
Browse files Browse the repository at this point in the history
  • Loading branch information
svqualitydev committed Jul 14, 2017
1 parent f9f366e commit 76c2a35
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/administrate/field/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ def initialize(attribute, data, page, options = {})
@attribute = attribute
@data = data
@page = page
@resource = options.delete(:resource)
@options = options
end

Expand All @@ -39,7 +40,7 @@ def to_partial_path
"/fields/#{self.class.field_type}/#{page}"
end

attr_reader :attribute, :data, :page
attr_reader :attribute, :data, :page, :resource

protected

Expand Down
3 changes: 2 additions & 1 deletion lib/administrate/field/deferred.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ def initialize(deferred_class, options = {})
attr_reader :deferred_class, :options

def new(*args)
deferred_class.new(*args, options)
new_options = args.last.respond_to?(:merge) ? args.pop : {}
deferred_class.new(*args, options.merge(new_options))
end

def ==(other)
Expand Down
2 changes: 1 addition & 1 deletion lib/administrate/page/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def resource_path
def attribute_field(dashboard, resource, attribute_name, page)
value = get_attribute_value(resource, attribute_name)
field = dashboard.attribute_type_for(attribute_name)
field.new(attribute_name, value, page)
field.new(attribute_name, value, page, resource: resource)
end

def get_attribute_value(resource, attribute_name)
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/pages/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,14 @@
expect(page.page_title).to eq("Worf")
end
end

describe "#attributes" do
it "passes the resource to the field object" do
customer = double(name: "Worf").as_null_object
page = Administrate::Page::Show.new(CustomerDashboard.new, customer)

expect(page.attributes.first.resource).to eq(customer)
expect(page.attributes.first.resource.name).to eq("Worf")
end
end
end

0 comments on commit 76c2a35

Please sign in to comment.