class Sunrise::AbstractModel

Attributes

resource_name[W]
available_list_view[RW]
current_list[RW]
model_name[RW]
sort_column[RW]

Public Class Methods

abstract_class?() click to toggle source
# File lib/sunrise/abstract_model.rb, line 32
def abstract_class?
  defined?(@abstract_class) && @abstract_class == true
end
config() click to toggle source
# File lib/sunrise/abstract_model.rb, line 24
def config
  @config ||= Config::Model.new(self)
end
method_missing(m, *args, &block) click to toggle source

Act as a proxy for the section configurations that actually store the configurations.

Calls superclass method
# File lib/sunrise/abstract_model.rb, line 38
def method_missing(m, *args, &block)
  if config.respond_to?(m)
    config.send(m, *args, &block)
  else
    super
  end
end
model() click to toggle source
# File lib/sunrise/abstract_model.rb, line 28
def model
  @model ||= Utils.lookup(resource_name.to_s.camelize)
end
new(params = {}) click to toggle source
# File lib/sunrise/abstract_model.rb, line 55
def initialize(params = {})
  @model_name = model.model_name
  @current_list = config.default_index_view
  @available_index_views = config.available_index_views
  @sort_column = config.sort_column
  @request_params = params.try(:symbolize_keys) || params
  self.current_list = params[:view]
end
resource_name() click to toggle source

Gets the resource_name

# File lib/sunrise/abstract_model.rb, line 12
def resource_name
  # Not using superclass_delegating_reader. See +site+ for explanation
  if defined?(@resource_name)
    @resource_name
  elsif superclass != Object && superclass.proxy
    superclass.proxy.dup.freeze
  end
end

Public Instance Methods

apply_scopes(params = nil, pagination = true) click to toggle source

Convert request params to model scopes

# File lib/sunrise/abstract_model.rb, line 163
def apply_scopes(params = nil, pagination = true)
  raise ::AbstractController::ActionNotFound, 'List config is turn off' if without_index?

  params ||= @request_params

  scope = default_scope(params)

  if current_list == :tree
    scope = scope.roots
  elsif pagination
    scope = page_scope(scope, params[:page], params[:per])
  end

  scope
end
association_scope() click to toggle source
# File lib/sunrise/abstract_model.rb, line 195
def association_scope
  parent_record&.send(parent_association.relation_name)
end
bottom_groups() click to toggle source

Find bottom groups

# File lib/sunrise/abstract_model.rb, line 275
def bottom_groups
  @bottom_groups ||= config.form.groups.values.select { |v| v.bottom? }
end
build_record() click to toggle source

Initialize new model, sets parent record and call build_defaults method

# File lib/sunrise/abstract_model.rb, line 155
def build_record
  record = model.new
  record.send("#{parent_association.name}=", parent_record) if parent_record
  record.build_defaults if record.respond_to?(:build_defaults)
  record
end
current_list=(value) click to toggle source

Save current list view

# File lib/sunrise/abstract_model.rb, line 69
def current_list=(value)
  @current_list = value.to_sym if value && @available_index_views.include?(value.to_sym)
end
default_scope(params = nil) click to toggle source

Apply default scopes: sort, search and association.

# File lib/sunrise/abstract_model.rb, line 180
def default_scope(params = nil)
  params ||= @request_params

  if model.respond_to?(:sunrise_search) && params[:search].present?
    scope = model.sunrise_search(params[:search])
  end
  scope ||= model.all

  scope = scope.merge(association_scope) unless parent_record.nil?
  scope = scope.merge(sort_scope(params[:sort])) if params[:sort].present?
  scope = scope.merge(list.scope) unless list.scope.nil?

  scope
end
destroy_all(params) click to toggle source
# File lib/sunrise/abstract_model.rb, line 125
def destroy_all(params)
  return if params[:ids].blank?

  run_callbacks :mass_destroy do
    model.where(id: params[:ids]).destroy_all
  end
end
export_columns() click to toggle source

List of columns names to be exported

# File lib/sunrise/abstract_model.rb, line 217
def export_columns
  @export_columns ||= (config.export ? config.export.fields.map(&:name) : model.column_names)
end
export_filename() click to toggle source

Filename for export data

# File lib/sunrise/abstract_model.rb, line 222
def export_filename
  @export_filename ||= [plural, Time.zone.now.strftime('%Y-%m-%d_%Hh%Mm%S')].join('_')
end
export_options() click to toggle source
# File lib/sunrise/abstract_model.rb, line 226
def export_options
  {
    filename: export_filename,
    columns: export_columns
  }
end
form_fields() click to toggle source
# File lib/sunrise/abstract_model.rb, line 233
def form_fields
  config.form.fields || []
end
list() click to toggle source

Get current list settings

# File lib/sunrise/abstract_model.rb, line 87
def list
  return false if without_index?

  config.index(current_list)
end
model_params() click to toggle source
# File lib/sunrise/abstract_model.rb, line 64
def model_params
  @request_params[param_key.to_sym] || {}
end
page_scope(scope, page = 1, per_page = nil) click to toggle source
# File lib/sunrise/abstract_model.rb, line 199
def page_scope(scope, page = 1, per_page = nil)
  page = 1 if page.blank? || page.to_i <= 0
  per_page ||= list.items_per_page

  scope.page(page).per(per_page)
end
parent_hash() click to toggle source

Convert parent id and class name into hash

# File lib/sunrise/abstract_model.rb, line 79
def parent_hash
  if parent_record
    @parent_hash ||= { parent_id: parent_record.id, parent_type: parent_record.class.name.underscore }
  end
  @parent_hash ||= {}
end
parent_record() click to toggle source

Load association record

# File lib/sunrise/abstract_model.rb, line 74
def parent_record
  @parent_record ||= find_parent_record
end
permit_attributes(params, user = nil) click to toggle source
# File lib/sunrise/abstract_model.rb, line 237
def permit_attributes(params, user = nil)
  value = config.form.permited_attributes

  attrs = case value
          when Proc then value.call(user)
          when String then value.to_sym
          else value
  end

  if attrs == :all
    params.require(param_key).permit!
  else
    attrs = Array.wrap(attrs).map(&:to_sym)
    params.require(param_key).permit(*attrs)
  end
end
search_available?() click to toggle source
# File lib/sunrise/abstract_model.rb, line 98
def search_available?
  list && !list.groups[:search].nil?
end
sidebar_groups() click to toggle source

Find sidebar groups

sidebar_groups?() click to toggle source

Check if sidebar groups exists

sort_available?() click to toggle source
# File lib/sunrise/abstract_model.rb, line 102
def sort_available?
  list && !list.groups[:sort].nil?
end
sort_fields() click to toggle source
# File lib/sunrise/abstract_model.rb, line 106
def sort_fields
  @sort_fields ||= list.groups[:sort].fields.each_with_object([]) do |field, items|
    [:desc, :asc].each do |direction|
      name = [field.name, direction].join('_')
      items << OpenStruct.new(name: I18n.t(name, scope: [:manage, :sort_columns]), value: name)
    end
  end
end
sort_scope(options = nil) click to toggle source
# File lib/sunrise/abstract_model.rb, line 206
def sort_scope(options = nil)
  options = Utils.sort_to_hash(options) if options.is_a?(String)
  options = { column: list.sort_column, mode: list.sort_mode }.merge(options || {})

  options[:column] = list.sort_column if options[:column].blank?
  options[:mode] = list.sort_mode     if options[:mode].blank?

  model.order([options[:column], options[:mode]].join(' '))
end
translate?() click to toggle source

Has translated columns

# File lib/sunrise/abstract_model.rb, line 255
def translate?
  config.form.groups[:translate].present?
end
translate_fields() click to toggle source

Files to translate

# File lib/sunrise/abstract_model.rb, line 260
def translate_fields
  config.form.groups[:translate].try(:fields) || []
end
update_sort(params) click to toggle source
# File lib/sunrise/abstract_model.rb, line 115
def update_sort(params)
  run_callbacks :sort do
    if params[:ids].present?
      update_sort_column(params[:ids])
    elsif params[:tree].present?
      update_sort_tree(params[:tree])
    end
  end
end
update_sort_column(ids) click to toggle source
# File lib/sunrise/abstract_model.rb, line 146
def update_sort_column(ids)
  return nil if ids.empty?

  ids.each do |key, value|
    model.where(id: key).update_all(@sort_column => value)
  end
end
update_sort_tree(ids) click to toggle source

Update nested tree {“id”=>{“parent_id”=>“root”, “depth”=>“0”, “left”=>“1”, “right”=>“22”}

# File lib/sunrise/abstract_model.rb, line 136
def update_sort_tree(ids)
  return nil if ids.empty?

  ids.each do |key, value|
    hash = { parent_id: nil, depth: value[:depth], lft: value[:left], rgt: value[:right] }
    hash[:parent_id] = value[:parent_id] unless value[:parent_id] == 'root'
    model.where(id: key).update_all(hash)
  end
end
without_index?() click to toggle source

Is index config disabled

# File lib/sunrise/abstract_model.rb, line 94
def without_index?
  config.index === false
end

Protected Instance Methods

find_parent_record() click to toggle source

Try to find parent object if any association present

# File lib/sunrise/abstract_model.rb, line 282
def find_parent_record
  parent_association.model.find(@request_params[:parent_id]) if parent_present? && parent_valid?
end
parent_association() click to toggle source

Find related association in model config

# File lib/sunrise/abstract_model.rb, line 287
def parent_association
  @parent_association ||= config.associations.detect { |relation| relation.is_this?(@request_params[:parent_type]) }
end
parent_present?() click to toggle source

Check parent record in request params

# File lib/sunrise/abstract_model.rb, line 297
def parent_present?
  !(@request_params[:parent_id].blank? || @request_params[:parent_type].blank?)
end
parent_valid?() click to toggle source

Parent association exists

# File lib/sunrise/abstract_model.rb, line 292
def parent_valid?
  !parent_association.nil?
end