Class: Debci::Admin

Inherits:
AuthApp show all
Includes:
HTMLHelpers
Defined in:
lib/debci/admin.rb

Defined Under Namespace

Classes: Model

Constant Summary

Constants included from HTMLHelpers

HTMLHelpers::ICONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HTMLHelpers

#expand_pin_packages, #expand_url, #file_link, #history_url, #icon, #package_url, #title_test_trigger_pin

Methods inherited from AuthApp

#authenticate!, #forbidden

Methods inherited from App

#get_page_params, get_page_range, #get_page_range, #pagination_params, #refresh!

Class Method Details

.model(name, **opts) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/debci/admin.rb', line 16

def self.model(name, **opts)
  model = Model.new(name)
  opts.each do |k, v|
    model.send("#{k}=", v)
  end
  settings.models[name] = model
end

Instance Method Details

#current_modelObject



115
116
117
# File 'lib/debci/admin.rb', line 115

def current_model
  @current_model ||= current_model_info.ar_class
end

#current_model_infoObject



111
112
113
# File 'lib/debci/admin.rb', line 111

def current_model_info
  @current_model_info ||= settings.models[params[:model].to_sym]
end

#current_model_nameObject



107
108
109
# File 'lib/debci/admin.rb', line 107

def current_model_name
  @current_model_name ||= params[:model]
end

#current_objectObject



123
124
125
126
127
# File 'lib/debci/admin.rb', line 123

def current_object
  @current_object ||= current_model.find(params[:id])
rescue ActiveRecord::RecordNotFound
  halt 404
end

#fieldsObject



129
130
131
# File 'lib/debci/admin.rb', line 129

def fields
  @fields ||= current_model_info.fields
end

#orderObject



119
120
121
# File 'lib/debci/admin.rb', line 119

def order
  current_model_info.order || :id
end

#read_only_fieldsObject



137
138
139
140
141
142
143
144
145
146
# File 'lib/debci/admin.rb', line 137

def read_only_fields
  @read_only_fields ||=
    if current_model_info.read_only_fields
      current_model_info.read_only_fields
    elsif current_model_info.edit_fields
      current_model_info.fields - current_model_info.edit_fields
    else
      []
    end
end

#save_object(object, params, read_only_fields) ⇒ Object



96
97
98
99
100
101
102
103
104
105
# File 'lib/debci/admin.rb', line 96

def save_object(object, params, read_only_fields)
  data = params.reject do |key, _value|
    [:model, :id].include?(key.to_sym) || read_only_fields.include?(key.to_sym)
  end
  data.each_key do |f|
    column = current_model.columns_hash[f]
    data[f] = nil if column.null && data[f].blank?
  end
  object.update!(data) # FIXME: handle validation failures
end

#search_fieldsObject



148
149
150
# File 'lib/debci/admin.rb', line 148

def search_fields
  @search_fields ||= current_model_info.search_fields
end

#select_fieldsObject



133
134
135
# File 'lib/debci/admin.rb', line 133

def select_fields
  @select_fields ||= current_model_info.select_fields || []
end