module CmAdmin::Models::DslMethod

def all_db_columns(options = {})

Other tags:
    Example: Getting all columns -

Parameters:
  • exclude (Array) -- the array of fields to exclude
def all_db_columns(options = {})
  field_names = instance_variable_get(:@ar_model)&.columns&.map { |x| x.name.to_sym }
  if options.include?(:exclude) && field_names
    excluded_fields = ([] << options[:exclude]).flatten.map(&:to_sym)
    field_names -= excluded_fields
  end
  field_names.each do |field_name|
    column field_name
  end
end

def bulk_action(name: nil, display_name: nil, display_if: ->(_arg) { true }, redirection_url: nil, icon_name: nil, verb: nil, display_type: nil, modal_configuration: {}, route_type: nil, partial: nil, &block)

Other tags:
    Example: Creating a bulk action -

Parameters:
  • partial (String) -- the partial path of action
  • route_type (String) -- the route type of action, +member+, +collection+
  • modal_configuration (Hash) -- the configuration of modal
  • display_type (Symbol) -- the display type of action, +:page+, +:modal+
  • verb (String) -- the verb of action, +get+, +post+, +put+, +patch+ or +delete+
  • icon_name (String) -- the icon name of action, follow font-awesome icon name
  • redirection_url (String) -- the redirection url of action
  • display_if (Proc) -- A lambda that takes the current object and return true or false
  • display_name (String) -- the display name of action
  • name (String) -- the name of action
def bulk_action(name: nil, display_name: nil, display_if: ->(_arg) { true }, redirection_url: nil, icon_name: nil, verb: nil, display_type: nil, modal_configuration: {}, route_type: nil, partial: nil, &block)
  bulk_action = CmAdmin::Models::BulkAction.new(
    name:, display_name:, display_if:, modal_configuration:,
    redirection_url:, icon_name:, action_type: :bulk_action,
    verb:, display_type:, route_type:, partial:, &block
  )
  @available_actions << bulk_action
end

def cm_edit(page_title: nil, page_description: nil, partial: nil, redirect_to: nil)

Other tags:
    Example: Editing page with a redirect -

Parameters:
  • redirect_to (Proc, nil) -- A lambda that takes the current object and redirect to path after update
  • partial (String) -- the partial path of page
  • page_description (String) -- the description of page
  • page_title (String) -- or [Symbol] the title of page, if symbol passed, it will be a method name on model
def cm_edit(page_title: nil, page_description: nil, partial: nil, redirect_to: nil)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'edit')
  @current_action.set_values(page_title, page_description, partial, redirect_to)
  yield
end

def cm_index(page_title: nil, page_description: nil, partial: nil, view_type: :table)

Other tags:
    Example: Index page -

Parameters:
  • view_type (Symbol) -- view type of page +:table+, +:card+ or +:kanban+
  • partial (String) -- the partial path of page
  • page_description (String) -- the description of page
  • page_title (String) -- the title of page
def cm_index(page_title: nil, page_description: nil, partial: nil, view_type: :table)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'index')
  @current_action.set_values(page_title, page_description, partial, view_type)
  yield
end

def cm_new(page_title: nil, page_description: nil, partial: nil, redirect_to: nil)

Other tags:
    Example: Creating a new page with a redirect -

Parameters:
  • redirect_to (Proc, nil) -- A lambda that takes the current object and redirect to path after create
  • partial (String) -- the partial path of page
  • page_description (String) -- the description of page
  • page_title (String) -- the title of page
def cm_new(page_title: nil, page_description: nil, partial: nil, redirect_to: nil)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'new')
  @current_action.set_values(page_title, page_description, partial, redirect_to)
  yield
end

def cm_section(section_name, display_if: nil, col_size: nil, html_attrs: nil, &block)

Other tags:
    Example: Creating a section -
def cm_section(section_name, display_if: nil, col_size: nil, html_attrs: nil, &block)
  @available_fields[@current_action.name.to_sym] ||= []
  @available_fields[@current_action.name.to_sym] << CmAdmin::Models::Section.new(section_name, @current_action, @model, display_if, html_attrs, col_size, &block)
end

def cm_show(page_title: nil, page_description: nil, partial: nil)

Other tags:
    Example: Showing page -

Parameters:
  • partial (String) -- the partial path of page
  • page_description (String) -- the description of page
  • page_title (String | Symbol) -- the title of page, if symbol passed, it will be a method name on model
def cm_show(page_title: nil, page_description: nil, partial: nil)
  @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
  @current_action.set_values(page_title, page_description, partial)
  yield
end

def cm_show_section(section_name, display_if: nil, html_attrs: nil, &block)

Deprecated:
  • Use {#cm_section} instead of this method
def cm_show_section(section_name, display_if: nil, html_attrs: nil, &block)
  cm_section(section_name, display_if:, html_attrs:, &block)
end

def column(field_name, options = {})

Other tags:
    Example: Creating a column -

Parameters:
  • width (Integer) -- the width of field for image field
  • height (Integer) -- the height of field for image field
  • helper_method (Symbol) -- the helper method for field, should be defined in custom_helper.rb file, will take two arguments, +record+ and +field_name+
  • format (String) -- the format of field for date field
  • header (String) -- the header of field
  • field_type (Symbol) -- the type of field, +:string+, +:text+, +:image+, +:date+, +:rich_text+, +:time+, +:integer+, +:decimal+, +:custom+, +:datetime+, +:money+, +:money_with_symbol+, +:link+, +:association+, +:enum+, +:tag+, +:attachment+, +:drawer+
  • field_name (String) -- the name of field
def column(field_name, options = {})
  @available_fields[@current_action.name.to_sym] ||= []
  raise 'Only one column can be locked in a table.' if @available_fields[@current_action.name.to_sym].select { |x| x.lockable }.size > 0 && options[:lockable]
  duplicate_columns = @available_fields[@current_action.name.to_sym].filter { |x| x.field_name.to_sym == field_name }
  terminate = false
  if duplicate_columns.size.positive?
    duplicate_columns.each do |column|
      if options[:field_type].to_s != 'association'
        terminate = true
      elsif options[:field_type].to_s == 'association' && column.association_name.to_s == options[:association_name].to_s
        terminate = true
      end
    end
  end
  return if terminate
  @available_fields[@current_action.name.to_sym] << CmAdmin::Models::Column.new(field_name, options)
end

def custom_action(name: nil, page_title: nil, page_description: nil, display_name: nil, verb: nil, layout: nil, layout_type: nil, partial: nil, path: nil, display_type: nil, modal_configuration: {}, url_params: {}, display_if: ->(_arg) { true }, route_type: nil, icon_name: 'fa fa-th-large', &block)

Other tags:
    Example: Creating a custom action with button -
    Example: Creating a custom action with modal -

Parameters:
  • icon_name (String) -- the icon name of action, follow font-awesome icon name
  • route_type (String) -- the route type of action, +member+, +collection+
  • display_if (Proc) -- A lambda that takes the current object and return true or false
  • url_params (Hash) -- the url params of action
  • modal_configuration (Hash) -- the configuration of modal
  • display_type (Symbol) -- the display type of action, +:button+, +:modal+
  • path (String) -- the path of action
  • partial (String) -- the partial path of action
  • layout_type (String) -- the layout type of action, +cm_association_index+, +cm_association_show+
  • layout (String) -- the layout of action
  • verb (String) -- the verb of action, +get+, +post+, +put+, +patch+ or +delete+
  • display_name (String) -- the display name of action
  • page_description (String) -- the description of page
  • page_title (String) -- the title of page
  • name (String) -- the name of action
def custom_action(name: nil, page_title: nil, page_description: nil, display_name: nil, verb: nil, layout: nil, layout_type: nil, partial: nil, path: nil, display_type: nil, modal_configuration: {}, url_params: {}, display_if: ->(_arg) { true }, route_type: nil, icon_name: 'fa fa-th-large', &block)
  action = CmAdmin::Models::CustomAction.new(
    page_title:, page_description:,
    name:, display_name:, verb:, layout:,
    layout_type:, partial:, path:,
    parent: current_action.name, display_type:, display_if:,
    action_type: :custom, route_type:, icon_name:, modal_configuration:,
    model_name: self.name, url_params:, &block
  )
  @available_actions << action
end

def filter(db_column_name, filter_type, options = {})

Other tags:
    Example: Creating a filter -

Parameters:
  • collection (Array) -- the collection of filter, use with single_select or multi_select
  • filter_with (Symbol) -- filter with scope name on model
  • helper_method (String) -- the helper method for filter, should be defined in custom_helper.rb file
  • placeholder (String) -- the placeholder of filter
  • filter_type (String) -- the type of filter, +:date+, +:multi_select+, +:range+, +:search+, +:single_select+
  • db_column_name (String) -- the name of column
def filter(db_column_name, filter_type, options = {})
  @filters << CmAdmin::Models::Filter.new(db_column_name:, filter_type:, options:)
end

def kanban_view(column_name, exclude: [], only: [])

Parameters:
  • only (Array) -- the array of fields to include
  • exclude (Array) -- the array of fields to exclude
  • column_name (String) -- the name of column
def kanban_view(column_name, exclude: [], only: [])
  return unless @current_action
  @current_action.kanban_attr[:column_name] = column_name
  @current_action.kanban_attr[:exclude] = exclude
  @current_action.kanban_attr[:only] = only
end

def page_description(description)

Parameters:
  • description (String) -- the description of page
def page_description(description)
  return unless @current_action
  @current_action.page_description = description
end

def page_title(title)

Parameters:
  • title (String) -- the title of page
def page_title(title)
  return unless @current_action
  @current_action.page_title = title
end

def row(display_if: nil, html_attrs: nil, &block)

Other tags:
    Example: Creating a row -

Parameters:
  • html_attrs (Hash) -- A hash that contains html attributes
  • display_if (Proc) -- A lambda that takes the current object and return true or false
def row(display_if: nil, html_attrs: nil, &block)
  @available_fields[@current_action.name.to_sym] ||= []
  @available_fields[@current_action.name.to_sym] << CmAdmin::Models::Row.new(@current_action, @model, display_if, html_attrs, &block)
end

def scope_list(scopes = [])

Parameters:
  • scopes (Array) -- the array of scopes
def scope_list(scopes = [])
  return unless @current_action
  @current_action.scopes = scopes
end

def sort_column(column = :created_at)

Other tags:
    Example: Setting sort column -

Parameters:
  • column (Symbol) -- the column name
def sort_column(column = :created_at)
  @current_action.sort_column = column.to_sym if @current_action
end

def sort_direction(direction = :desc)

Other tags:
    Example: Setting sort direction -

Parameters:
  • direction (Symbol) -- the direction of sort, +:asc+, +:desc+
def sort_direction(direction = :desc)
  raise ArgumentError, "Select a valid sort direction like #{CmAdmin::Models::Action::VALID_SORT_DIRECTION.join(' or ')} instead of #{direction}" unless CmAdmin::Models::Action::VALID_SORT_DIRECTION.include?(direction.to_sym.downcase)
  @current_action.sort_direction = direction.to_sym if @current_action
end

def tab(tab_name, custom_action, associated_model: nil, layout_type: nil, layout: nil, partial: nil, display_if: nil, &block)

Other tags:
    Example: Creating a tab -

Parameters:
  • display_if (Proc) -- A lambda that takes the current object and return true or false
  • partial (String) -- the partial path of tab
  • layout (String) -- the layout of tab
  • layout_type (String) -- the layout type of tab, +cm_association_index+, +cm_association_show+
  • associated_model (String) -- the name of associated model
  • custom_action (String) -- the name of custom action
  • tab_name (String) -- or [Symbol] the name of tab
def tab(tab_name, custom_action, associated_model: nil, layout_type: nil, layout: nil, partial: nil, display_if: nil, &block)
  if custom_action.to_s == ''
    @current_action = CmAdmin::Models::Action.find_by(self, name: 'show')
    @available_tabs << CmAdmin::Models::Tab.new(tab_name, '', display_if, &block)
  else
    action = CmAdmin::Models::Action.new(name: custom_action.to_s, verb: :get, path: ':id/' + custom_action,
                                         layout_type:, layout:, partial:, child_records: associated_model,
                                         action_type: :custom, display_type: :page, model_name: name)
    @available_actions << action
    @current_action = action
    @available_tabs << CmAdmin::Models::Tab.new(tab_name, custom_action, display_if, &block)
  end
  yield if block
end