Skip to content

Instantly share code, notes, and snippets.

@coop
Created August 9, 2012 09:58
Show Gist options
  • Select an option

  • Save coop/3302824 to your computer and use it in GitHub Desktop.

Select an option

Save coop/3302824 to your computer and use it in GitHub Desktop.
module API
module Private
class IncomingController < ApplicationController
attr_writer :payload
respond_to :json
def receive
key = payload.keys.first
object = {'charity' => Charity}.fetch(key).new payload[key]
object.save
end
def payload
@payload ||= ActiveSupport::JSON.decode params[:payload]
end
class Charity
attr_accessor :name, :persistance
def initialize attributes = {}
self.persistance = attributes.delete(:persistance) { ::Charity.new }
attributes.slice(*accepted_attributes).each do |attr, value|
self.send "#{attr}=", value
end if attributes
end
def accepted_attributes
[:name]
end
def save
persistance.name = name
persistance.save
end
end
end
end
end
@coop
Copy link
Author

coop commented Aug 9, 2012

This controller is responsible for taking an incoming request from a known source. Instead of using attr_accessible I am thinking of introducing a class that sits between the incoming request and the persistance model.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment