class CmAdmin::Model

Attributes

actions_set[RW]
ar_model[R]
available_actions[RW]
available_fields[RW]
current_action[RW]
filters[RW]
name[R]
params[RW]
permitted_fields[RW]

Public Class Methods

all_actions() click to toggle source
# File lib/cm_admin/model.rb, line 39
def all_actions
  @all_actions || []
end
find_by(search_hash) click to toggle source
# File lib/cm_admin/model.rb, line 193
def self.find_by(search_hash)
  CmAdmin.cm_admin_models.find { |x| x.name == search_hash[:name] }
end
new(entity, &block) click to toggle source
# File lib/cm_admin/model.rb, line 23
def initialize(entity, &block)
  @name = entity.name
  @ar_model = entity
  @available_actions ||= []
  @current_action = nil
  @available_fields ||= {index: [], show: [], edit: [], new: []}
  @params = nil
  @filters ||= []
  instance_eval(&block) if block_given?
  actions unless @actions_set
  $available_actions = @available_actions.dup
  self.class.all_actions.push(@available_actions)
  define_controller
end

Public Instance Methods

actions(only: [], except: []) click to toggle source

Insert into actions according to config block

# File lib/cm_admin/model.rb, line 45
def actions(only: [], except: [])
  acts = CmAdmin::DEFAULT_ACTIONS.keys
  acts = acts & (Array.new << only).flatten if only.present?
  acts = acts - (Array.new << except).flatten if except.present?
  acts.each do |act|
    action_defaults = CmAdmin::DEFAULT_ACTIONS[act]
    @available_actions << CmAdmin::Models::Action.new(name: act.to_s, verb: action_defaults[:verb], path: action_defaults[:path])
  end
  @actions_set = true
end
all_db_columns(options={}) click to toggle source
# File lib/cm_admin/model.rb, line 182
def all_db_columns(options={})
  field_names = self.instance_variable_get(:@ar_model)&.columns&.map{|x| x.name.to_sym}
  if options.include?(:exclude) && field_names
    excluded_fields = (Array.new << options[:exclude]).flatten.map(&:to_sym)
    field_names -= excluded_fields
  end
  field_names.each do |field_name|
    column field_name
  end
end
cm_edit(&block) click to toggle source
# File lib/cm_admin/model.rb, line 70
def cm_edit(&block)
  action = CmAdmin::Models::Action.find_by(self, name: 'edit')
  action.instance_eval(&block)
end
cm_index() { || ... } click to toggle source
# File lib/cm_admin/model.rb, line 64
def cm_index(&block)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'index')
  yield
  # action.instance_eval(&block)
end
cm_show() { || ... } click to toggle source
# File lib/cm_admin/model.rb, line 56
def cm_show(&block)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
  puts "Top of the line"
  yield
  # action.instance_eval(&block)
  puts "End of the line"
end
column(field_name, options={}) click to toggle source
# File lib/cm_admin/model.rb, line 175
def column(field_name, options={})
  unless @available_fields[:index].map{|x| x.db_column_name.to_sym}.include?(field_name)
    puts "For printing column #{field_name}"
    @available_fields[:index] << CmAdmin::Models::Column.new(field_name, options)
  end
end
create(params) click to toggle source
# File lib/cm_admin/model.rb, line 149
def create(params)
  @ar_object = @ar_model.new(resource_params(params))
end
custom_action(name: nil, verb: nil, layout: nil, partial: nil, path: nil, &block) click to toggle source

Custom actions eg class User < ApplicationRecord

cm_admin do
  custom_action name: 'submit', verb: 'post', path: ':id/submit' do
    def user_submit
      Code for action here...
    end
  end
end

end

# File lib/cm_admin/model.rb, line 208
def custom_action(name: nil, verb: nil, layout: nil, partial: nil, path: nil, &block)
  @available_actions << CmAdmin::Models::Action.new(name: name, verb: verb, layout: layout, partial: partial, path: path)
  self.class.class_eval(&block)
end
edit(params) click to toggle source
# File lib/cm_admin/model.rb, line 140
def edit(params)
  @ar_object = @ar_model.find(params[:id])
end
field(field_name, options={}) click to toggle source
# File lib/cm_admin/model.rb, line 170
def field(field_name, options={})
  puts "For printing field #{field_name}"
  @available_fields[:show] << CmAdmin::Models::Field.new(field_name, options)
end
filter(db_column_name, filter_type, options={}) click to toggle source
# File lib/cm_admin/model.rb, line 213
def filter(db_column_name, filter_type, options={})
  @filters << CmAdmin::Models::Filter.new(db_column_name: db_column_name, filter_type: filter_type, options: options)
end
filter_by(params, filter_params={}, sort_params={}) click to toggle source
# File lib/cm_admin/model.rb, line 86
def filter_by(params, filter_params={}, sort_params={})
  filtered_result = OpenStruct.new
  sort_column = "users.created_at"
  sort_direction = %w[asc desc].include?(sort_params[:sort_direction]) ? sort_params[:sort_direction] : "asc"
  sort_params = {sort_column: sort_column, sort_direction: sort_direction}
  final_data = filtered_data(filter_params)
  pagy, records = pagy(final_data)
  filtered_result.data = records
  filtered_result.pagy = pagy
  # filtered_result.facets = paginate(page, raw_data.size)
  # filtered_result.sort = sort_params
  # filtered_result.facets.sort = sort_params
  return filtered_result
end
filtered_data(filter_params) click to toggle source
# File lib/cm_admin/model.rb, line 101
def filtered_data(filter_params)
  records = self.name.constantize.where(nil)
  if filter_params
    filter_params.each do |scope, scope_value|
      records = self.send("cm_#{scope}", scope_value, records)
    end
  end
  records
end
index(params) click to toggle source
# File lib/cm_admin/model.rb, line 80
def index(params)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'index')
  # Based on the params the filter and pagination object to be set
  @ar_object = filter_by(params, filter_params=filter_params(params))
end
new(params) click to toggle source
# File lib/cm_admin/model.rb, line 136
def new(params)
  @ar_object = @ar_model.new
end
page_description(description) click to toggle source
# File lib/cm_admin/model.rb, line 164
def page_description(description)
  if @current_action
    @current_action.page_description = description
  end
end
page_title(title) click to toggle source
# File lib/cm_admin/model.rb, line 158
def page_title(title)
  if @current_action
    @current_action.page_title = title
  end
end
resource_params(params) click to toggle source
# File lib/cm_admin/model.rb, line 153
def resource_params(params)
  permittable_fields = @permitted_fields || @ar_model.columns.map(&:name).reject { |i| CmAdmin::REJECTABLE_FIELDS.include?(i) }.map(&:to_sym)
  params.require(self.name.underscore.to_sym).permit(*permittable_fields)
end
show(params) click to toggle source
# File lib/cm_admin/model.rb, line 75
def show(params)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
  @ar_object = @ar_model.find(params[:id])
end
update(params) click to toggle source
# File lib/cm_admin/model.rb, line 144
def update(params)
  @ar_object = @ar_model.find(params[:id])
  @ar_object.update(resource_params(params))
end

Private Instance Methods

define_controller() click to toggle source

Controller defined for each model If model is User, controller will be UsersController

# File lib/cm_admin/model.rb, line 220
def define_controller
  klass = Class.new(CmAdmin::ApplicationController) do

    $available_actions.each do |action|
      define_method action.name.to_sym do

        # controller_name & action_name from ActionController
        @model = CmAdmin::Model.find_by(name: controller_name.classify)
        @model.params = params
        @action = CmAdmin::Models::Action.find_by(@model, name: action_name)
        @ar_object = @model.send(action_name, params)
        respond_to do |format|
          if %w(show index new edit).include?(action_name)
            if request.xhr? && action_name.eql?('index')
              format.html { render partial: '/cm_admin/main/table' }
            else
              format.html { render '/cm_admin/main/'+action_name }
            end
          elsif %w(create update destroy).include?(action_name)
            if @ar_object.save
              format.html { redirect_to  CmAdmin::Engine.mount_path + "/#{@model.name.underscore.pluralize}" }
            else
              format.html { render '/cm_admin/main/new' }
            end
          elsif action.layout.present?
            if action.partial.present?
              render partial: action.partial, layout: action.layout
            else
              render layout: action.layout
            end
          end
        end
      end
    end
  end if $available_actions.present?
  CmAdmin.const_set "#{@name}Controller", klass
end
filter_params(params) click to toggle source
# File lib/cm_admin/model.rb, line 258
def filter_params(params)
  params.require(:filters).permit(:search) if params[:filters]
end