Created
May 20, 2013 17:26
-
-
Save guipdutra/5613743 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 CrudController < ApplicationController | |
before_filter :choose_session_descriptor, :except => [:modal] | |
before_filter :authorize_resource! | |
inherit_resources | |
respond_to :js, :json | |
has_scope :filter, :type => :hash | |
has_scope :page, :default => 1, :only => [:index, :modal], :unless => :disable_pagination? | |
has_scope :per, :default => 10, :only => [:index, :modal], :unless => :disable_pagination? | |
has_scope :with_entity_id, | |
:default => Proc.new { |c| c.current_entity.id }, | |
:if => Proc.new { |c| c.resource_class.reflect_on_all_associations.select { |e| e.name == :descriptor }.any? } | |
has_scope :with_year, | |
:default => Proc.new { |c| c.current_year }, | |
:if => Proc.new { |c| c.resource_class.reflect_on_all_associations.select { |e| e.name == :descriptor }.any? } | |
rescue_from ActiveRecord::RecordNotFound do |exception| | |
if @current_scopes && @current_scopes.include?(:with_entity_id) && @current_scopes.include?(:with_year) | |
Raven.capture_exception(exception) if capture_exceptions? | |
render 'public/401', :formats => [:html], :status => 401, :layout => false | |
else | |
raise | |
end | |
end | |
public :resource_class | |
custom_actions :collection => [:filter, :modal], :member => :modal_info | |
helper_method :main_controller_name, :nested? | |
def create | |
create! { collection_path } | |
end | |
def update | |
update! { collection_path } | |
end | |
def destroy | |
destroy! do |success, failure| | |
failure.html do | |
# FIXME: I'm not sure about why flash is lost here but | |
# if we don't keep, the failure message is not shown. | |
flash.keep | |
redirect_to edit_resource_path | |
end | |
end | |
end | |
def filter | |
object = build_resource | |
object.assign_attributes(params[:filter]) | |
render :layout => false | |
end | |
def modal | |
build_resource | |
render :layout => false | |
end | |
def modal_info | |
show! | |
end | |
def nested? | |
main_controller_name != controller_name | |
end | |
protected | |
def smart_resource_path | |
path = nil | |
if respond_to? :show | |
path = resource_path rescue nil | |
end | |
path ||= smart_collection_path | |
end | |
helper_method :smart_resource_path | |
def smart_collection_path | |
path = nil | |
if respond_to? :index | |
path ||= collection_path rescue nil | |
end | |
if respond_to? :parent | |
path ||= parent_path rescue nil | |
end | |
path ||= root_path rescue nil | |
end | |
helper_method :smart_collection_path | |
def authorize_resource! | |
authorize! action_name, main_controller_name | |
end | |
def main_controller_name | |
MainControllerGetter.new(controller_name).name | |
end | |
# Build resource using I18n::Alchemy | |
def build_resource | |
get_resource_ivar || set_resource_ivar(effectively_build_resource) | |
end | |
# Effectively build resource using I18n::Alchemy | |
def effectively_build_resource | |
end_of_association_chain.send(method_for_build).tap do |object| | |
object.localized.assign_attributes(*resource_params) | |
end | |
end | |
# Update resource using I18n::Alchemy | |
def update_resource(object, attributes) | |
object.localized.update_attributes(*attributes) | |
end | |
# Get collection using ordered scope | |
def collection | |
get_collection_ivar || set_collection_ivar(end_of_association_chain.ordered) | |
end | |
def disable_pagination? | |
params[:page] == 'all' | |
end | |
private | |
def capture_exceptions? | |
%w[production staging].include? ENV['RACK_ENV'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment