Module: ActiveScaffold::Core
- Defined in:
- lib/active_scaffold/core.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
- .active_record_column_type_cast(value, column_or_type) ⇒ Object
-
.active_scaffold_controller_for(klass, controller_namespace = '::') ⇒ Object
Tries to find a controller for the given ActiveRecord model.
- .column_type_cast(value, column) ⇒ Object
- .included(base) ⇒ Object
- .mongoid_column_type_cast(value, column) ⇒ Object
Instance Method Summary collapse
- #active_scaffold_config ⇒ Object
- #active_scaffold_session_storage(id = nil) ⇒ Object
- #active_scaffold_session_storage_key(id = nil) ⇒ Object
- #setup_user_settings ⇒ Object
- #user_settings_storage ⇒ Object
Class Method Details
.active_record_column_type_cast(value, column_or_type) ⇒ Object
271 272 273 274 275 276 |
# File 'lib/active_scaffold/core.rb', line 271 def self.active_record_column_type_cast(value, column_or_type) return Time.zone.at(value.to_i) if %i[time datetime].include?(column_or_type.type) && value =~ /\A\d+\z/ cast_type = column_or_type.is_a?(ActiveRecord::ConnectionAdapters::Column) ? ActiveRecord::Type.lookup(column_or_type.type) : column_or_type cast_type ? cast_type.cast(value) : value end |
.active_scaffold_controller_for(klass, controller_namespace = '::') ⇒ Object
Tries to find a controller for the given ActiveRecord model. Searches in the namespace of the current controller for singular and plural versions of the conventional “#modelController” syntax. You may override this method to customize the search routine.
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 |
# File 'lib/active_scaffold/core.rb', line 228 def self.active_scaffold_controller_for(klass, controller_namespace = '::') = [] class_names = [klass.to_s, klass.to_s.demodulize].map { |k| k.underscore.pluralize }.map { |k| [k, k.singularize] }.flatten [controller_namespace, ''].each do |namespace| class_names.each do |controller_name| begin controller = "#{namespace}#{controller_name.camelize}Controller".constantize rescue NameError => e # Only rescue NameError associated with the controller constant not existing - not other compile errors raise unless e.["uninitialized constant #{controller}"] << "#{namespace}#{controller_name.camelize}Controller" next end raise ActiveScaffold::ControllerNotFound, "#{controller} missing ActiveScaffold", caller unless controller.uses_active_scaffold? unless controller.active_scaffold_config.model.to_s == klass.to_s raise ActiveScaffold::ControllerNotFound, "ActiveScaffold on #{controller} is not for #{klass} model.", caller end return controller end end raise ActiveScaffold::ControllerNotFound, "Could not find #{.join(' or ')}", caller end |
.column_type_cast(value, column) ⇒ Object
253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/active_scaffold/core.rb', line 253 def self.column_type_cast(value, column) if defined?(ActiveRecord) && column.is_a?(ActiveRecord::ConnectionAdapters::Column) active_record_column_type_cast(value, column) elsif defined?(ActiveModel) && column.is_a?(ActiveModel::Attribute) active_record_column_type_cast(value, column.type) elsif defined?(Mongoid) && column.is_a?(Mongoid::Fields::Standard) mongoid_column_type_cast(value, column) else value end end |
.included(base) ⇒ Object
3 4 5 |
# File 'lib/active_scaffold/core.rb', line 3 def self.included(base) base.extend(ClassMethods) end |
.mongoid_column_type_cast(value, column) ⇒ Object
265 266 267 268 269 |
# File 'lib/active_scaffold/core.rb', line 265 def self.mongoid_column_type_cast(value, column) return Time.zone.at(value.to_i) if [Time, DateTime].include?(column.type) && value =~ /\A\d+\z/ column.type.evolve value end |
Instance Method Details
#active_scaffold_config ⇒ Object
12 13 14 15 16 17 |
# File 'lib/active_scaffold/core.rb', line 12 def active_scaffold_config @active_scaffold_config ||= begin setup_user_settings unless self.class.active_scaffold_config.user self.class.active_scaffold_config.user end end |
#active_scaffold_session_storage(id = nil) ⇒ Object
24 25 26 27 |
# File 'lib/active_scaffold/core.rb', line 24 def active_scaffold_session_storage(id = nil) session_index = active_scaffold_session_storage_key(id) session[session_index] ||= {} end |
#active_scaffold_session_storage_key(id = nil) ⇒ Object
19 20 21 22 |
# File 'lib/active_scaffold/core.rb', line 19 def active_scaffold_session_storage_key(id = nil) id ||= params[:eid] || "#{params[:controller]}#{"_#{nested_parent_id}" if nested?}" "as:#{id}" end |
#setup_user_settings ⇒ Object
7 8 9 10 |
# File 'lib/active_scaffold/core.rb', line 7 def setup_user_settings config = self.class.active_scaffold_config config.new_user_settings(user_settings_storage, params) end |
#user_settings_storage ⇒ Object
29 30 31 32 33 34 35 |
# File 'lib/active_scaffold/core.rb', line 29 def user_settings_storage if self.class.active_scaffold_config.store_user_settings active_scaffold_session_storage else {} end end |