Last active
December 10, 2015 14:29
-
-
Save adrianpike/4448172 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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