module ExpressAdmin::StandardActions::InstanceMethods
Public Instance Methods
create()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 101 def create build_resource(resource_params) if resource.save respond_to do |format| format.html { redirect_to after_create_path } format.js { render create_view, status: :created } end else respond_to do |format| format.html { render new_view, layout: new_layout } format.js { render create_view, status: :unprocessable_entity } end end end
destroy()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 146 def destroy load_resource resource.destroy! respond_to do |format| format.html { redirect_to after_destroy_path } end end
edit()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 124 def edit load_resource respond_to do |format| format.html { render edit_view, layout: edit_layout } end end
index()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 86 def index build_resource load_collection respond_to do |format| format.html { render index_view, layout: index_layout } end end
new()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 94 def new build_resource respond_to do |format| format.html { render new_view, layout: new_layout } end end
show()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 116 def show load_resource load_collection respond_to do |format| format.html { render show_view, layout: show_layout } end end
update()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 131 def update load_resource if resource.update_attributes(resource_params) respond_to do |format| format.html { redirect_to after_update_path } format.js { render update_view, status: :ok } end else respond_to do |format| format.html { render edit_view, layout: edit_layout } format.js { render update_view, status: :unprocessable_entity } end end end
Protected Instance Methods
_array_filter(list)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 298 def _array_filter(list) list.select {|item| item.send(_filter_field).eql?(_filter_value)} end
_filter_field()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 290 def _filter_field params[:filter] ? params[:filter].keys.first : nil end
_filter_value()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 294 def _filter_value params[:filter][_filter_field] end
_sql_filter(scope)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 302 def _sql_filter(scope) scope.where(_filter_field.to_sym => _filter_value) end
after_create_path()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 160 def after_create_path resource_path end
after_destroy_path()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 164 def after_destroy_path collection_path end
after_update_path()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 156 def after_update_path resource_path end
build_resource(*args)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 341 def build_resource(*args) if nested? self.instance_variable_set(resource_ivar, end_of_association_chain.send(collection_name).build(*args)) else self.instance_variable_set(resource_ivar, resource_class.new(*args)) end end
collection()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 329 def collection self.instance_variable_get(collection_ivar) end
collection_ivar()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 325 def collection_ivar "@#{collection_name}".to_sym end
collection_name()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 333 def collection_name self.class.to_s.demodulize.gsub(/Controller$/, '').pluralize.underscore end
collection_path()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 215 def collection_path proxy = route_proxy proxy ||= self if parent_resource_names.blank? proxy.send(scoped_path_helper("#{collection_name}_path")) else proxy.send(scoped_path_helper(nested_collection_path_helper), resource_ids_hash) end end
create_view()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 184 def create_view :create end
default_layout()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 192 def default_layout defaults[:layout] end
edit_view()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 168 def edit_view :edit end
end_of_association_chain()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 259 def end_of_association_chain if nested? parent_resource else nil end end
expose_parent_resources()
click to toggle source
defines a current_<something> for each parent resource in a nested resource situation
# File lib/express_admin/standard_actions.rb, line 372 def expose_parent_resources parent_resources = parent_resource_names return if parent_resources.blank? previous_parent = nil parent_resources.each do |parent_name| # TODO: optimize parent_id = extract_path_info_from_routes["#{parent_name}_id".to_sym] current_parent = "current_#{parent_name}".to_sym unless self.methods.include?(current_parent) if previous_parent.nil? self.class_eval do define_method(current_parent) do current_class_name = parent_name.camelize parent_module = parent_module_name.constantize rescue nil current_class = if parent_module && # try to find the class in the parent module parent_module.const_defined?(current_class_name) parent_module.const_get(current_class_name) else "::#{current_class_name.camelize}".constantize end current_class.find(parent_id) end end else self.class_eval do grandparent_accessor = previous_parent define_method(current_parent) do grandparent_resource = self.send(grandparent_accessor) parent_scope = grandparent_resource.send(parent_name.pluralize) parent_scope.find(parent_id) end end end end previous_parent = current_parent end end
extract_path_info_from_application_routes()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 421 def extract_path_info_from_application_routes begin recognized_path = Rails.application.routes.recognize_path(request.path) rescue ActionController::RoutingError => e end end
extract_path_info_from_engine_routes()
click to toggle source
TODO: to accurately reproduce the fall through matching we need to do this in order of mounting within the application routes.
# File lib/express_admin/standard_actions.rb, line 431 def extract_path_info_from_engine_routes Rails::Engine.subclasses.each do |engine| engine_instance = engine.instance engine_route = Rails.application.routes.routes.find do |r| r.app.app == engine_instance.class end next unless engine_route path_for_engine = request.path.gsub(%r(^#{engine_route.path.spec.to_s}), "") begin recognized_path = engine_instance.routes.recognize_path(path_for_engine) rescue ActionController::RoutingError => e end end end
extract_path_info_from_routes()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 446 def extract_path_info_from_routes extract_path_info_from_application_routes || extract_path_info_from_engine_routes end
filter(scope)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 282 def filter(scope) if _filter_field scope.kind_of?(Array) ? _array_filter(scope) : _sql_filter(scope) else scope end end
index_view()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 180 def index_view :index end
load_collection()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 267 def load_collection scope = if end_of_association_chain end_of_association_chain.send(collection_name) else resource_class end filtered_scope = filter(ordered_scope(scope)) # TODO: Removed the conditionally invoked .all # scope = params[:asc] || params[:desc] ? sort_table(scope) : scope self.instance_variable_set(collection_ivar, filtered_scope) end
load_resource()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 349 def load_resource self.instance_variable_set(resource_ivar, resource_class.find(params[:id])) end
nested?()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 251 def nested? !parent_resource_names.empty? end
nested_collection_path_helper()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 366 def nested_collection_path_helper (parent_resource_names + [collection_name, 'path']).join('_') end
nested_resource_path_helper()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 362 def nested_resource_path_helper (parent_resource_names + [resource_name, 'path']).join('_') end
new_view()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 172 def new_view :new end
ordered_scope(scope)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 278 def ordered_scope(scope) scope end
parent_module_name()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 225 def parent_module_name self.class.to_s.match(/(\w+)::/).try(:[], 1) end
parent_resource()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 255 def parent_resource self.send("current_#{parent_resource_names.last}") end
parent_resource_names()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 412 def parent_resource_names path_info = extract_path_info_from_routes unless path_info.nil? path_info.keys.grep(/_id$/).map do |id| id.to_s.gsub(/_id$/, '') end end end
resource()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 337 def resource self.instance_variable_get(resource_ivar) end
resource_ids_hash()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 451 def resource_ids_hash parent_resource_names.inject({id: resource.to_param}) do |hash, name| hash["#{name}_id".to_sym] = self.send("current_#{name}".to_sym).to_param hash end end
resource_ivar()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 353 def resource_ivar "@#{resource_name}".to_sym end
resource_name()
click to toggle source
Foo::WidgetsController -> widget
# File lib/express_admin/standard_actions.rb, line 358 def resource_name self.class.resource_name end
resource_params()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 247 def resource_params self.send("#{resource_name}_params") end
resource_path()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 201 def resource_path proxy = route_proxy proxy ||= self if parent_resource_names.blank? proxy.send(scoped_path_helper("#{resource_name}_path"), resource) else proxy.send(scoped_path_helper(nested_resource_path_helper), resource_ids_hash) end end
route_proxy()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 229 def route_proxy engine = "#{parent_module_name}::Engine".constantize rescue nil if parent_module_name && engine self.send(parent_module_name.underscore) else nil end end
scope_name()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 238 def scope_name engine = "#{parent_module_name}::Engine".constantize rescue nil if parent_module_name && engine.nil? return parent_module_name.underscore else nil end end
scoped_path_helper(path_helper)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 211 def scoped_path_helper(path_helper) [scope_name, path_helper].compact.join('_') end
search(scope)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 321 def search(scope) Search.execute(scope, params[:search_string]) end
search?(scope)
click to toggle source
TODO: Refactor.
# File lib/express_admin/standard_actions.rb, line 317 def search?(scope) params[:search_string].present? ? search(scope) : scope end
show_view()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 176 def show_view :show end
sort_table(scope)
click to toggle source
# File lib/express_admin/standard_actions.rb, line 306 def sort_table(scope) if params.keys.include?('asc') scope.sort{ |a, b| a.send(params[:asc]) <=> b.send(params[:asc])} elsif params.keys.include?('desc') scope.sort{ |a, b| b.send(params[:desc]) <=> a.send(params[:desc])} else scope end end
update_view()
click to toggle source
# File lib/express_admin/standard_actions.rb, line 188 def update_view :udpate end