Skip to content

Instantly share code, notes, and snippets.

@jeroenvandijk
Created July 30, 2009 16:03
Show Gist options
  • Save jeroenvandijk/158752 to your computer and use it in GitHub Desktop.
Save jeroenvandijk/158752 to your computer and use it in GitHub Desktop.
class ResourceController < ApplicationController
acts_as_resource
# Set flashes to I18n
create.flash { translate_flash :create }
create.fails.flash { translate_flash :create_fails }
update.flash { translate_flash :update }
update.fails.flash { translate_flash :update_fails }
destroy.flash { translate_flash :destroy }
def self.options(options = {})
@options ||= options
end
# Override of ResourceLogic's default
def collection
@collection ||= load_search_and_collection
end
before_filter :set_instance_variables
def set_instance_variables
# Administrator
@selected_link_partners_count = LinkPartner.link_partner_order_members_selected_equals(true).count
end
private
# Gives flashes a default translation
def translate_flash(action)
key = "flash.#{action}"
translate("#{controller_name}.#{key}", :default => translate("default.#{key}", :model => model_name.to_s.classify.constantize.human_name ) )
end
# Loads object through the given search
# TODO rails does something weird when named_scopes are called on collections
# it duplicates SQL and this causes collisions sometimes. So scope isn't used but the parent_id is left instead
#
def load_search_and_collection
# strip all unnessary params
keys_to_strip = %w(controller action)
keys_to_strip = parent_model_name.to_s.foreign_key if parent?
stripped_params = params.dup.slice!(*keys_to_strip)
if parent?
if object.respond_to?(parent_model_name)
stripped_params["#{parent_model_name}_id_equals"] = parent_object.id
else
stripped_params["#{parent_model_name.to_s.pluralize}_id_equals"] = parent_object.id
end
end
# stripped_params["#{parent_key_name}_equals"] = params[parent_key_name]
# move the search params one level lower
stripped_params = unnest_hashes(stripped_params, :do_not_prefix => "search")
# Only include param that have a corresponding method
stripped_params.reject!{|key, _| !model.condition?(key) } # use model instead of 'scope' because of the above reason
@search = model.scoped(:group => 'id' ).search(stripped_params) # Group is added to prevent double TODO this really should unnessary
self.class.options[:paginate] ? @search.paginate(:all, :page => params[:page], :per_page => self.class.options[:per_page]) : @search.all
end
# Flattens hashes so they can be used by search logic
def unnest_hashes(*args)
options = args.extract_options!
source = args.shift
target = args.shift || {}.with_indifferent_access
source.each_pair do |key, value|
if value.is_a? Hash
prefix = key unless [options[:do_not_prefix]].include?(key)
nested = unnest_hashes(value, target, :prefix => prefix)
else
new_key = [options[:prefix], key].compact.join("_")
target[new_key] = value
end
end
target
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment