Class: Sunrise::AbstractModel
- Inherits:
-
Object
- Object
- Sunrise::AbstractModel
- Extended by:
- ActiveModel::Callbacks
- Defined in:
- lib/sunrise/abstract_model.rb
Direct Known Subclasses
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
-
.resource_name ⇒ Object
Gets the resource_name.
-
.resource_name=(name) ⇒ Object
Set resource_name.
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.
56 57 58 59 60 61 62 63 |
# File 'lib/sunrise/abstract_model.rb', line 56 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.symbolize_keys self.current_list = params[:view] end |
Instance Attribute Details
#available_list_view ⇒ Object
Returns the value of attribute available_list_view.
48 49 50 |
# File 'lib/sunrise/abstract_model.rb', line 48 def available_list_view @available_list_view end |
#current_list ⇒ Object
Returns the value of attribute current_list.
48 49 50 |
# File 'lib/sunrise/abstract_model.rb', line 48 def current_list @current_list end |
#model_name ⇒ Object
Returns the value of attribute model_name.
48 49 50 |
# File 'lib/sunrise/abstract_model.rb', line 48 def model_name @model_name end |
#sort_column ⇒ Object
Returns the value of attribute sort_column.
48 49 50 |
# File 'lib/sunrise/abstract_model.rb', line 48 def sort_column @sort_column end |
Class Method Details
.abstract_class? ⇒ Boolean
33 34 35 |
# File 'lib/sunrise/abstract_model.rb', line 33 def abstract_class? defined?(@abstract_class) && @abstract_class == true end |
.config ⇒ Object
25 26 27 |
# File 'lib/sunrise/abstract_model.rb', line 25 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.
39 40 41 42 43 44 45 |
# File 'lib/sunrise/abstract_model.rb', line 39 def method_missing(m, *args, &block) if config.respond_to?(m) config.send(m, *args, &block) else super end end |
.model ⇒ Object
29 30 31 |
# File 'lib/sunrise/abstract_model.rb', line 29 def model @model ||= Utils.lookup(resource_name.to_s.camelize) end |
.resource_name ⇒ Object
Gets the resource_name
11 12 13 14 15 16 17 18 |
# File 'lib/sunrise/abstract_model.rb', line 11 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 |
.resource_name=(name) ⇒ Object
Set resource_name
21 22 23 |
# File 'lib/sunrise/abstract_model.rb', line 21 def resource_name=(name) @resource_name = name end |
Instance Method Details
#apply_scopes(params = nil, pagination = true) ⇒ Object
Convert request params to model scopes
165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/sunrise/abstract_model.rb', line 165 def apply_scopes(params = nil, pagination = true) raise ::AbstractController::ActionNotFound.new("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
194 195 196 197 198 |
# File 'lib/sunrise/abstract_model.rb', line 194 def association_scope if parent_record parent_record.send(parent_association.relation_name) end end |
#bottom_groups ⇒ Object
Find bottom groups
276 277 278 |
# File 'lib/sunrise/abstract_model.rb', line 276 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
157 158 159 160 161 162 |
# File 'lib/sunrise/abstract_model.rb', line 157 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.
181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/sunrise/abstract_model.rb', line 181 def default_scope(params = nil) params ||= @request_params scope = model.sunrise_search(params[:search]) if model.respond_to?(:sunrise_search) && !params[:search].blank? scope ||= model.where(nil) scope = scope.merge(association_scope) unless parent_record.nil? scope = scope.merge(sort_scope(params[:sort])) unless params[:sort].blank? scope = scope.merge(list.scope) unless list.scope.nil? scope end |
#destroy_all(params) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/sunrise/abstract_model.rb', line 127 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
218 219 220 |
# File 'lib/sunrise/abstract_model.rb', line 218 def export_columns @export_columns ||= (config.export ? config.export.fields.map(&:name) : model.column_names) end |
#export_filename ⇒ Object
Filename for export data
223 224 225 |
# File 'lib/sunrise/abstract_model.rb', line 223 def export_filename @export_filename ||= [plural, Time.now.strftime("%Y-%m-%d_%Hh%Mm%S")].join('_') end |
#export_options ⇒ Object
227 228 229 230 231 232 |
# File 'lib/sunrise/abstract_model.rb', line 227 def { :filename => export_filename, :columns => export_columns } end |
#form_fields ⇒ Object
234 235 236 |
# File 'lib/sunrise/abstract_model.rb', line 234 def form_fields config.form.fields || [] end |
#list ⇒ Object
Get current list settings
88 89 90 91 |
# File 'lib/sunrise/abstract_model.rb', line 88 def list return false if without_index? config.index(current_list) end |
#model_params ⇒ Object
65 66 67 |
# File 'lib/sunrise/abstract_model.rb', line 65 def model_params @request_params[param_key.to_sym] end |
#page_scope(scope, page = 1, per_page = nil) ⇒ Object
200 201 202 203 204 205 |
# File 'lib/sunrise/abstract_model.rb', line 200 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
82 83 84 85 |
# File 'lib/sunrise/abstract_model.rb', line 82 def parent_hash @parent_hash ||= { :parent_id => parent_record.id, :parent_type => parent_record.class.name.underscore } if parent_record @parent_hash ||= {} end |
#parent_record ⇒ Object
Load association record
77 78 79 |
# File 'lib/sunrise/abstract_model.rb', line 77 def parent_record @parent_record ||= find_parent_record end |
#permit_attributes(params, user = nil) ⇒ Object
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
# File 'lib/sunrise/abstract_model.rb', line 238 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
266 267 268 |
# File 'lib/sunrise/abstract_model.rb', line 266 def @sidebar_groups ||= config.form.groups.values.select { |v| v. } end |
#sidebar_groups? ⇒ Boolean
Check if sidebar groups exists
271 272 273 |
# File 'lib/sunrise/abstract_model.rb', line 271 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 114 115 |
# File 'lib/sunrise/abstract_model.rb', line 106 def sort_fields @sort_fields ||= list.groups[:sort].fields.inject([]) do |items, field| [:desc, :asc].each do |direction| name = [field.name, direction].join('_') items << OpenStruct.new(:name => I18n.t(name, :scope => [:manage, :sort_columns]), :value => name) end items end end |
#sort_scope(options = nil) ⇒ Object
207 208 209 210 211 212 213 214 215 |
# File 'lib/sunrise/abstract_model.rb', line 207 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
256 257 258 |
# File 'lib/sunrise/abstract_model.rb', line 256 def translate? !config.form.groups[:translate].blank? end |
#translate_fields ⇒ Object
Files to translate
261 262 263 |
# File 'lib/sunrise/abstract_model.rb', line 261 def translate_fields config.form.groups[:translate].try(:fields) || [] end |
#update_sort(params) ⇒ Object
117 118 119 120 121 122 123 124 125 |
# File 'lib/sunrise/abstract_model.rb', line 117 def update_sort(params) run_callbacks :sort do if !params[:ids].blank? update_sort_column(params[:ids]) elsif !params[:tree].blank? update_sort_tree(params[:tree]) end end end |
#update_sort_column(ids) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/sunrise/abstract_model.rb', line 148 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”
138 139 140 141 142 143 144 145 146 |
# File 'lib/sunrise/abstract_model.rb', line 138 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 |