Class: CmAdmin::Model

Inherits:
Object
  • Object
show all
Includes:
CmAdmin::Models::Blocks, CmAdmin::Models::DslMethod, Pagy::Backend
Defined in:
lib/cm_admin/model.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CmAdmin::Models::DslMethod

#all_db_columns, #cm_edit, #cm_index, #cm_new, #cm_show, #cm_show_section, #column, #custom_action, #filter, #form_field, #nested_form_field, #page_description, #page_title, #sort_column, #sort_direction, #tab

Methods included from CmAdmin::Models::Blocks

#set_menu, #set_title

Constructor Details

#initialize(entity, &block) ⇒ Model

Returns a new instance of Model.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cm_admin/model.rb', line 30

def initialize(entity, &block)
  @name = entity.name
  @ar_model = entity
  @is_visible_on_sidebar = true
  @icon_name = 'fa fa-th-large'
  @available_actions ||= []
  @current_action = nil
  @available_tabs ||= []
  @available_fields ||= {index: [], show: [], edit: {fields: []}, new: {fields: []}}
  @params = nil
  @filters ||= []
  instance_eval(&block) if block_given?
  actions unless @actions_set
  $available_actions = @available_actions.dup
  define_controller
end

Instance Attribute Details

#actions_setObject

Returns the value of attribute actions_set.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def actions_set
  @actions_set
end

#ar_modelObject (readonly)

Returns the value of attribute ar_model.



28
29
30
# File 'lib/cm_admin/model.rb', line 28

def ar_model
  @ar_model
end

#available_actionsObject

Returns the value of attribute available_actions.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def available_actions
  @available_actions
end

#available_fieldsObject

Returns the value of attribute available_fields.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def available_fields
  @available_fields
end

#available_tabsObject

Returns the value of attribute available_tabs.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def available_tabs
  @available_tabs
end

#current_actionObject

Returns the value of attribute current_action.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def current_action
  @current_action
end

#filtersObject

Returns the value of attribute filters.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def filters
  @filters
end

#icon_nameObject

Returns the value of attribute icon_name.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def icon_name
  @icon_name
end

#importerObject (readonly)

Returns the value of attribute importer.



28
29
30
# File 'lib/cm_admin/model.rb', line 28

def importer
  @importer
end

#is_visible_on_sidebarObject (readonly)

Returns the value of attribute is_visible_on_sidebar.



28
29
30
# File 'lib/cm_admin/model.rb', line 28

def is_visible_on_sidebar
  @is_visible_on_sidebar
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/cm_admin/model.rb', line 28

def name
  @name
end

#paramsObject

Returns the value of attribute params.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def params
  @params
end

#permitted_fieldsObject

Returns the value of attribute permitted_fields.



26
27
28
# File 'lib/cm_admin/model.rb', line 26

def permitted_fields
  @permitted_fields
end

Class Method Details

.find_by(search_hash) ⇒ Object



49
50
51
# File 'lib/cm_admin/model.rb', line 49

def find_by(search_hash)
  CmAdmin.config.cm_admin_models.find { |x| x.name == search_hash[:name] }
end

Instance Method Details

#actions(only: [], except: []) ⇒ Object

Insert into actions according to config block



74
75
76
77
78
79
80
81
82
83
# File 'lib/cm_admin/model.rb', line 74

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

#custom_controller_action(action_name, params) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/cm_admin/model.rb', line 54

def custom_controller_action(action_name, params)
  current_action = CmAdmin::Models::Action.find_by(self, name: action_name.to_s)
  if current_action
    @current_action = current_action
    @ar_object = @ar_model.name.classify.constantize.find(params[:id])
    if @current_action.child_records
      child_records = @ar_object.send(@current_action.child_records)
      @associated_model = CmAdmin::Model.find_by(name: @ar_model.reflect_on_association(@current_action.child_records).klass.name)
      if child_records.is_a? ActiveRecord::Relation
        @associated_ar_object = filter_by(params, child_records)
      else
        @associated_ar_object = child_records
      end
      return @ar_object, @associated_model, @associated_ar_object
    end
    return @ar_object
  end
end

#filter_params(params) ⇒ Object

Shared between export controller and resource controller



98
99
100
101
102
103
104
105
106
# File 'lib/cm_admin/model.rb', line 98

def filter_params(params)
  # OPTIMIZE: Need to check if we can permit the filter_params in a better way
  date_columns = self.filters.select{|x| x.filter_type.eql?(:date)}.map(&:db_column_name)
  range_columns = self.filters.select{|x| x.filter_type.eql?(:range)}.map(&:db_column_name)
  single_select_columns = self.filters.select{|x| x.filter_type.eql?(:single_select)}.map(&:db_column_name)
  multi_select_columns = self.filters.select{|x| x.filter_type.eql?(:multi_select)}.map{|x| Hash["#{x.db_column_name}", []]}

  params.require(:filters).permit(:search, date: date_columns, range: range_columns, single_select: single_select_columns, multi_select: multi_select_columns) if params[:filters]
end

#importable(class_name:, importer_type:) ⇒ Object



85
86
87
# File 'lib/cm_admin/model.rb', line 85

def importable(class_name:, importer_type:)
  @importer = CmAdmin::Models::Importer.new(class_name, importer_type)
end

#set_icon(name) ⇒ Object



93
94
95
# File 'lib/cm_admin/model.rb', line 93

def set_icon(name)
  @icon_name = name
end

#visible_on_sidebar(visible_option) ⇒ Object



89
90
91
# File 'lib/cm_admin/model.rb', line 89

def visible_on_sidebar(visible_option)
  @is_visible_on_sidebar = visible_option
end