diff --git a/lib/administrate/field/base.rb b/lib/administrate/field/base.rb index de2cbdfc..507222f8 100644 --- a/lib/administrate/field/base.rb +++ b/lib/administrate/field/base.rb @@ -20,6 +20,7 @@ def initialize(attribute, data, page, options = {}) @attribute = attribute @data = data @page = page + @resource = options.delete(:resource) @options = options end @@ -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 diff --git a/lib/administrate/field/deferred.rb b/lib/administrate/field/deferred.rb index 40dfc266..a5107bc0 100644 --- a/lib/administrate/field/deferred.rb +++ b/lib/administrate/field/deferred.rb @@ -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) diff --git a/lib/administrate/page/base.rb b/lib/administrate/page/base.rb index 00770ead..5d32815d 100644 --- a/lib/administrate/page/base.rb +++ b/lib/administrate/page/base.rb @@ -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) diff --git a/spec/lib/pages/show_spec.rb b/spec/lib/pages/show_spec.rb index c7a03802..f1d783f2 100644 --- a/spec/lib/pages/show_spec.rb +++ b/spec/lib/pages/show_spec.rb @@ -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