Skip to content

Instantly share code, notes, and snippets.

@demental
Last active August 29, 2015 14:18
Show Gist options
  • Save demental/7855fd4f25dcbc909dd8 to your computer and use it in GitHub Desktop.
Save demental/7855fd4f25dcbc909dd8 to your computer and use it in GitHub Desktop.
class ReviewsController < ApplicationController
def edit
@review = Reviews::Submitter.new(params).model
end
def update
submitter = Reviews::Submitter.new(params)
@review = submitter.model
submitter.on(:success) { |review| redirect_to success_reviews_path }
submitter.on(:failure) { |review| render action: :edit }
submitter.call
end
end
class Review < ActiveRecord::Base
belongs_to :psy, class_name: 'User'
belongs_to :patient, class_name: 'User'
end
module Reviews
class Submitter
class Form
include ActiveModel::Model
include Virtus.model
attribute :content, String
attribute :age, Integer
attribute :gender, String
attribute :submitted_at, DateTime, writer: :private, default: lambda { |review, attribute| Time.zone.now }
validates :content, presence: true
validates :age, presence: true, inclusion: { in: 12..115 }
validates :gender, presence: true, inclusion: { in: ['M','F'] }
end
include Wisper::Publisher
attr_reader :params
def initialize(params)
@params = params
end
def call
if form.valid?
model.update_attributes form.attributes
broadcast(:success, model)
broadcast(:review_submitted, model)
else
broadcast(:failure, model)
end
end
def model
@model ||= Review.where(token: params[:id], submitted_at: nil).take!
end
def form
@form ||= Form.new(params[:review])
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment