Created
August 9, 2012 09:58
-
-
Save coop/3302824 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
| 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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.