Class: Sunrise::AbstractModel
- Inherits:
-
Object
- Object
- Sunrise::AbstractModel
- Extended by:
- ActiveModel::Callbacks
- Defined in:
- lib/sunrise/abstract_model.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.resource_name ⇒ Object
Gets the resource_name.
Instance Attribute Summary collapse
-
#available_list_view ⇒ Object
Returns the value of attribute available_list_view.
-
#current_list ⇒ Object
Returns the value of attribute current_list.
-
#model_name ⇒ Object
Returns the value of attribute model_name.
-
#sort_column ⇒ Object
Returns the value of attribute sort_column.
Class Method Summary collapse
- .abstract_class? ⇒ Boolean
- .config ⇒ Object
-
.method_missing(m, *args, &block) ⇒ Object
Act as a proxy for the section configurations that actually store the configurations.
- .model ⇒ Object
Instance Method Summary collapse
-
#apply_scopes(params = nil, pagination = true) ⇒ Object
Convert request params to model scopes.
- #association_scope ⇒ Object
-
#bottom_groups ⇒ Object
Find bottom groups.
-
#build_record ⇒ Object
Initialize new model, sets parent record and call build_defaults method.
-
#default_scope(params = nil) ⇒ Object
Apply default scopes: sort, search and association.
- #destroy_all(params) ⇒ Object
-
#export_columns ⇒ Object
List of columns names to be exported.
-
#export_filename ⇒ Object
Filename for export data.
- #export_options ⇒ Object
- #form_fields ⇒ Object
-
#initialize(params = {}) ⇒ AbstractModel
constructor
A new instance of AbstractModel.
-
#list ⇒ Object
Get current list settings.
- #model_params ⇒ Object
- #page_scope(scope, page = 1, per_page = nil) ⇒ Object
-
#parent_hash ⇒ Object
Convert parent id and class name into hash.
-
#parent_record ⇒ Object
Load association record.
- #permit_attributes(params, user = nil) ⇒ Object
- #search_available? ⇒ Boolean
-
#sidebar_groups ⇒ Object
Find sidebar groups.
-
#sidebar_groups? ⇒ Boolean
Check if sidebar groups exists.
- #sort_available? ⇒ Boolean
- #sort_fields ⇒ Object
- #sort_scope(options = nil) ⇒ Object
-
#translate? ⇒ Boolean
Has translated columns.
-
#translate_fields ⇒ Object
Files to translate.
- #update_sort(params) ⇒ Object
- #update_sort_column(ids) ⇒ Object
-
#update_sort_tree(ids) ⇒ Object
Update nested tree “depth”=>“0”, “left”=>“1”, “right”=>“22”.
-
#without_index? ⇒ Boolean
Is index config disabled.
Constructor Details
#initialize(params = {}) ⇒ AbstractModel
Returns a new instance of AbstractModel.
55 56 57 58 59 60 61 62 |
# 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 |
Class Attribute Details
.resource_name ⇒ Object
Gets the resource_name
12 13 14 15 16 17 18 19 |
# 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 |
Instance Attribute Details
#available_list_view ⇒ Object
Returns the value of attribute available_list_view.
47 48 49 |
# File 'lib/sunrise/abstract_model.rb', line 47 def available_list_view @available_list_view end |
#current_list ⇒ Object
Returns the value of attribute current_list.
47 48 49 |
# File 'lib/sunrise/abstract_model.rb', line 47 def current_list @current_list end |
#model_name ⇒ Object
Returns the value of attribute model_name.
47 48 49 |
# File 'lib/sunrise/abstract_model.rb', line 47 def model_name @model_name end |
#sort_column ⇒ Object
Returns the value of attribute sort_column.
47 48 49 |
# File 'lib/sunrise/abstract_model.rb', line 47 def sort_column @sort_column end |
Class Method Details
.abstract_class? ⇒ Boolean
32 33 34 |
# File 'lib/sunrise/abstract_model.rb', line 32 def abstract_class? defined?(@abstract_class) && @abstract_class == true end |
.config ⇒ Object
24 25 26 |
# File 'lib/sunrise/abstract_model.rb', line 24 def config @config ||= Config::Model.new(self) end |
.method_missing(m, *args, &block) ⇒ Object
Act as a proxy for the section configurations that actually store the configurations.
38 39 40 41 42 43 44 |
# 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 |
Instance Method Details
#apply_scopes(params = nil, pagination = true) ⇒ Object
Convert request params to model scopes
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 |
# 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 ⇒ Object
195 196 197 |
# File 'lib/sunrise/abstract_model.rb', line 195 def association_scope parent_record&.send(parent_association.relation_name) end |
#bottom_groups ⇒ Object
Find bottom groups
275 276 277 |
# File 'lib/sunrise/abstract_model.rb', line 275 def bottom_groups @bottom_groups ||= config.form.groups.values.select { |v| v.bottom? } end |
#build_record ⇒ Object
Initialize new model, sets parent record and call build_defaults method
155 156 157 158 159 160 |
# 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 |
#default_scope(params = nil) ⇒ Object
Apply default scopes: sort, search and association.
180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# 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) ⇒ Object
125 126 127 128 129 130 131 |
# 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 ⇒ Object
List of columns names to be exported
217 218 219 |
# 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 ⇒ Object
Filename for export data
222 223 224 |
# 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 ⇒ Object
226 227 228 229 230 231 |
# File 'lib/sunrise/abstract_model.rb', line 226 def { filename: export_filename, columns: export_columns } end |
#form_fields ⇒ Object
233 234 235 |
# File 'lib/sunrise/abstract_model.rb', line 233 def form_fields config.form.fields || [] end |
#list ⇒ Object
Get current list settings
87 88 89 90 91 |
# File 'lib/sunrise/abstract_model.rb', line 87 def list return false if without_index? config.index(current_list) end |
#model_params ⇒ Object
64 65 66 |
# 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) ⇒ Object
199 200 201 202 203 204 |
# 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 ⇒ Object
Convert parent id and class name into hash
79 80 81 82 83 84 |
# 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 ⇒ Object
Load association record
74 75 76 |
# File 'lib/sunrise/abstract_model.rb', line 74 def parent_record @parent_record ||= find_parent_record end |
#permit_attributes(params, user = nil) ⇒ Object
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 |
# 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? ⇒ Boolean
98 99 100 |
# File 'lib/sunrise/abstract_model.rb', line 98 def search_available? list && !list.groups[:search].nil? end |
#sidebar_groups ⇒ Object
Find sidebar groups
265 266 267 |
# File 'lib/sunrise/abstract_model.rb', line 265 def @sidebar_groups ||= config.form.groups.values.select { |v| v. } end |
#sidebar_groups? ⇒ Boolean
Check if sidebar groups exists
270 271 272 |
# File 'lib/sunrise/abstract_model.rb', line 270 def !.empty? end |
#sort_available? ⇒ Boolean
102 103 104 |
# File 'lib/sunrise/abstract_model.rb', line 102 def sort_available? list && !list.groups[:sort].nil? end |
#sort_fields ⇒ Object
106 107 108 109 110 111 112 113 |
# 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) ⇒ Object
206 207 208 209 210 211 212 213 214 |
# File 'lib/sunrise/abstract_model.rb', line 206 def sort_scope( = nil) = Utils.sort_to_hash() if .is_a?(String) = { column: list.sort_column, mode: list.sort_mode }.merge( || {}) [:column] = list.sort_column if [:column].blank? [:mode] = list.sort_mode if [:mode].blank? model.order([[:column], [:mode]].join(' ')) end |
#translate? ⇒ Boolean
Has translated columns
255 256 257 |
# File 'lib/sunrise/abstract_model.rb', line 255 def translate? config.form.groups[:translate].present? end |
#translate_fields ⇒ Object
Files to translate
260 261 262 |
# File 'lib/sunrise/abstract_model.rb', line 260 def translate_fields config.form.groups[:translate].try(:fields) || [] end |
#update_sort(params) ⇒ Object
115 116 117 118 119 120 121 122 123 |
# 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) ⇒ Object
146 147 148 149 150 151 152 |
# 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) ⇒ Object
Update nested tree “depth”=>“0”, “left”=>“1”, “right”=>“22”
136 137 138 139 140 141 142 143 144 |
# 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? ⇒ Boolean
Is index config disabled
94 95 96 |
# File 'lib/sunrise/abstract_model.rb', line 94 def without_index? config.index === false end |