Skip to content

Instantly share code, notes, and snippets.

@adrianpike
Last active December 10, 2015 14:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adrianpike/4448172 to your computer and use it in GitHub Desktop.
Save adrianpike/4448172 to your computer and use it in GitHub Desktop.
class MagicalPresenter < Draper::Base
extend ActiveModel::Naming
include ActiveModel::Conversion
attr_reader :changes
def initialize(model, opts = {})
@changes = {}
super(model)
end
delegate :id, to: :model
delegate :persisted?, to: :model
def child
model.foobar || Foobar.new(:parent => foobar) # GOTCHA: make sure and set the inverse on the relation, so the parent gets its foobar set
end
def child_location=(attrib)
child.location = attrib
end
def child_location
child.location # might be able to lean on delegate here
end
def update_attributes(attributes)
attributes.each { |key, value|
self.send("#{key}=", value.presence) # because draper, these will get passed through to the parent model
}
end
end
def update
@model = MagicalPresenter.new(parent.find(params[:id]))
@model.update_attributes(params[:model])
@model.save
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment