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) ⇒ 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
- #edit_fields ⇒ Object
-
#export_columns ⇒ Object
List of columns names to be exported.
-
#export_filename ⇒ Object
Filename for export data.
-
#initialize(params = {}) ⇒ AbstractModel
constructor
A new instance of AbstractModel.
-
#list ⇒ Object
Get current list settings.
-
#list_key ⇒ Object
Key which stored 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_list? ⇒ Boolean
Is list 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_list_view @available_list_view = config.available_list_view @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) ⇒ Object
Convert request params to model scopes
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/sunrise/abstract_model.rb', line 170 def apply_scopes(params = nil) raise ::AbstractController::ActionNotFound.new("List config is turn off") if without_list? params ||= @request_params scope = default_scope(params) if current_list == :tree scope = scope.roots else scope = page_scope(scope, params[:page], params[:per]) end scope end |
#association_scope ⇒ Object
199 200 201 202 203 |
# File 'lib/sunrise/abstract_model.rb', line 199 def association_scope if parent_record parent_record.send(parent_association.relation_name).scoped end end |
#bottom_groups ⇒ Object
Find bottom groups
274 275 276 |
# File 'lib/sunrise/abstract_model.rb', line 274 def bottom_groups @bottom_groups ||= config.edit.groups.values.select { |v| v.bottom? } end |
#build_record ⇒ Object
Initialize new model, sets parent record and call build_defaults method
162 163 164 165 166 167 |
# File 'lib/sunrise/abstract_model.rb', line 162 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.
186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/sunrise/abstract_model.rb', line 186 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.scoped 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
132 133 134 135 136 137 138 |
# File 'lib/sunrise/abstract_model.rb', line 132 def destroy_all(params) return if params[:ids].blank? run_callbacks :mass_destroy do model.where(:id => params[:ids]).destroy_all end end |
#edit_fields ⇒ Object
232 233 234 |
# File 'lib/sunrise/abstract_model.rb', line 232 def edit_fields config.edit.fields || [] end |
#export_columns ⇒ Object
List of columns names to be exported
223 224 225 |
# File 'lib/sunrise/abstract_model.rb', line 223 def export_columns list.fields ? list.fields.map(&:name) : model.column_names end |
#export_filename ⇒ Object
Filename for export data
228 229 230 |
# File 'lib/sunrise/abstract_model.rb', line 228 def export_filename @export_filename ||= [plural, Time.now.strftime("%Y-%m-%d_%Hh%Mm%S")].join('_') end |
#list ⇒ Object
Get current list settings
93 94 95 96 |
# File 'lib/sunrise/abstract_model.rb', line 93 def list return false if without_list? config.sections[list_key] ||= Config::List.new(self.class) end |
#list_key ⇒ Object
Key which stored list settings
77 78 79 |
# File 'lib/sunrise/abstract_model.rb', line 77 def list_key ["list", current_list].compact.join('_').to_sym 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
205 206 207 208 209 210 |
# File 'lib/sunrise/abstract_model.rb', line 205 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
87 88 89 90 |
# File 'lib/sunrise/abstract_model.rb', line 87 def parent_hash @parent_hash ||= { :parent_id => parent_record.id, :parent_type => parent_record.class.name } if parent_record @parent_hash ||= {} end |
#parent_record ⇒ Object
Load association record
82 83 84 |
# File 'lib/sunrise/abstract_model.rb', line 82 def parent_record @parent_record ||= find_parent_record end |
#permit_attributes(params, user = nil) ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/sunrise/abstract_model.rb', line 236 def permit_attributes(params, user = nil) value = config.edit.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
103 104 105 |
# File 'lib/sunrise/abstract_model.rb', line 103 def search_available? list && !list.groups[:search].nil? end |
#sidebar_groups ⇒ Object
Find sidebar groups
264 265 266 |
# File 'lib/sunrise/abstract_model.rb', line 264 def @sidebar_groups ||= config.edit.groups.values.select { |v| v. } end |
#sidebar_groups? ⇒ Boolean
Check if sidebar groups exists
269 270 271 |
# File 'lib/sunrise/abstract_model.rb', line 269 def !.empty? end |
#sort_available? ⇒ Boolean
107 108 109 |
# File 'lib/sunrise/abstract_model.rb', line 107 def sort_available? list && !list.groups[:sort].nil? end |
#sort_fields ⇒ Object
111 112 113 114 115 116 117 118 119 120 |
# File 'lib/sunrise/abstract_model.rb', line 111 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
212 213 214 215 216 217 218 219 220 |
# File 'lib/sunrise/abstract_model.rb', line 212 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
254 255 256 |
# File 'lib/sunrise/abstract_model.rb', line 254 def translate? !config.edit.groups[:translate].blank? end |
#translate_fields ⇒ Object
Files to translate
259 260 261 |
# File 'lib/sunrise/abstract_model.rb', line 259 def translate_fields config.edit.groups[:translate].try(:fields) || [] end |
#update_sort(params) ⇒ Object
122 123 124 125 126 127 128 129 130 |
# File 'lib/sunrise/abstract_model.rb', line 122 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
153 154 155 156 157 158 159 |
# File 'lib/sunrise/abstract_model.rb', line 153 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”
143 144 145 146 147 148 149 150 151 |
# File 'lib/sunrise/abstract_model.rb', line 143 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_list? ⇒ Boolean
Is list config disabled
99 100 101 |
# File 'lib/sunrise/abstract_model.rb', line 99 def without_list? config.sections[:list] === false end |