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
- #permit_model_params(*filters) ⇒ 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
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/sunrise/abstract_model.rb', line 173 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
205 206 207 |
# File 'lib/sunrise/abstract_model.rb', line 205 def association_scope parent_record&.send(parent_association.relation_name) end |
#bottom_groups ⇒ Object
Find bottom groups
285 286 287 |
# File 'lib/sunrise/abstract_model.rb', line 285 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
165 166 167 168 169 170 |
# File 'lib/sunrise/abstract_model.rb', line 165 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.
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/sunrise/abstract_model.rb', line 190 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
135 136 137 138 139 140 141 |
# File 'lib/sunrise/abstract_model.rb', line 135 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
227 228 229 |
# File 'lib/sunrise/abstract_model.rb', line 227 def export_columns @export_columns ||= (config.export ? config.export.fields.map(&:name) : model.column_names) end |
#export_filename ⇒ Object
Filename for export data
232 233 234 |
# File 'lib/sunrise/abstract_model.rb', line 232 def export_filename @export_filename ||= [plural, Time.zone.now.strftime('%Y-%m-%d_%Hh%Mm%S')].join('_') end |
#export_options ⇒ Object
236 237 238 239 240 241 |
# File 'lib/sunrise/abstract_model.rb', line 236 def { filename: export_filename, columns: export_columns } end |
#form_fields ⇒ Object
243 244 245 |
# File 'lib/sunrise/abstract_model.rb', line 243 def form_fields config.form.fields || [] end |
#list ⇒ Object
Get current list settings
97 98 99 100 101 |
# File 'lib/sunrise/abstract_model.rb', line 97 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
209 210 211 212 213 214 |
# File 'lib/sunrise/abstract_model.rb', line 209 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
89 90 91 92 93 94 |
# File 'lib/sunrise/abstract_model.rb', line 89 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
84 85 86 |
# File 'lib/sunrise/abstract_model.rb', line 84 def parent_record @parent_record ||= find_parent_record end |
#permit_attributes(params, user = nil) ⇒ Object
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/sunrise/abstract_model.rb', line 247 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 |
#permit_model_params(*filters) ⇒ Object
68 69 70 71 72 73 74 75 76 |
# File 'lib/sunrise/abstract_model.rb', line 68 def permit_model_params(*filters) params = @request_params[param_key.to_sym] if params filters.empty? ? params.permit! : params.permit(*filters) else {} end end |
#search_available? ⇒ Boolean
108 109 110 |
# File 'lib/sunrise/abstract_model.rb', line 108 def search_available? list && !list.groups[:search].nil? end |
#sidebar_groups ⇒ Object
Find sidebar groups
275 276 277 |
# File 'lib/sunrise/abstract_model.rb', line 275 def @sidebar_groups ||= config.form.groups.values.select { |v| v. } end |
#sidebar_groups? ⇒ Boolean
Check if sidebar groups exists
280 281 282 |
# File 'lib/sunrise/abstract_model.rb', line 280 def !.empty? end |
#sort_available? ⇒ Boolean
112 113 114 |
# File 'lib/sunrise/abstract_model.rb', line 112 def sort_available? list && !list.groups[:sort].nil? end |
#sort_fields ⇒ Object
116 117 118 119 120 121 122 123 |
# File 'lib/sunrise/abstract_model.rb', line 116 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
216 217 218 219 220 221 222 223 224 |
# File 'lib/sunrise/abstract_model.rb', line 216 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
265 266 267 |
# File 'lib/sunrise/abstract_model.rb', line 265 def translate? config.form.groups[:translate].present? end |
#translate_fields ⇒ Object
Files to translate
270 271 272 |
# File 'lib/sunrise/abstract_model.rb', line 270 def translate_fields config.form.groups[:translate].try(:fields) || [] end |
#update_sort(params) ⇒ Object
125 126 127 128 129 130 131 132 133 |
# File 'lib/sunrise/abstract_model.rb', line 125 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
156 157 158 159 160 161 162 |
# File 'lib/sunrise/abstract_model.rb', line 156 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"
146 147 148 149 150 151 152 153 154 |
# File 'lib/sunrise/abstract_model.rb', line 146 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
104 105 106 |
# File 'lib/sunrise/abstract_model.rb', line 104 def without_index? config.index === false end |