module EasyAdminUi module CoreExt def self.included(base) base.extend ClassMethods end module ClassMethods def easy_admin(admin_options = {}) self.easy_options = admin_options self.easy_options[:include] ||= '' make_resourceful do actions :all response_for :index do |format| format.html do @options = self.easy_options override = File.join('app/views', self.controller_path, 'index.html.erb') if @items.blank? && params[:page] && params[:page].to_i > 1 redirect_to :action => :index else if File.exists?(override) render :template => override.gsub('app/views', '') else render :template => 'easy_admin_ui/index' end end end end response_for :show do |format| format.html do @options = self.easy_options override = File.join(controller_full_path, 'show.html.erb') if File.exists?(override) render :template => override.gsub('app/views', '') else render :template => 'easy_admin_ui/show' end end end [:new, :create_fails].each do |new_action| response_for new_action do |format| format.html do override = File.join(controller_full_path, 'new.html.erb') if File.exists?(override) render :template => override.gsub('app/views', '') else render :template => 'easy_admin_ui/new' end end end end [:edit, :update_fails].each do |edit_action| response_for edit_action do |format| format.html do override = File.join(controller_full_path, 'edit.html.erb') if File.exists?(override) render :template => override.gsub('app/views', '') else render :template => 'easy_admin_ui/edit' end end end end response_for :destroy do |format| format.js do render :template => 'easy_admin_ui/destroy' end end eval(admin_options[:include]) end class_eval do def current_objects if defined?(Mongoid) && current_model.included_modules.include?(Mongoid::Document) @items = current_model.send(easy_options[:order_method] ? easy_options[:order_method] : :asc, easy_options[:order]).page(params[:page]).per(easy_options[:per_page]) else @items = current_model.order(easy_options[:order]).page(params[:page]).per(easy_options[:per_page]) end end def current_object @item ||= current_model.find(params[:id]) if params[:id] @item ||= current_model.new(params[current_model_name.underscore.to_sym]) end def instance_variable_name 'item' end helper_method :controller_full_path def controller_full_path File.join 'app/views', self.controller_path end end end end end end