Module: ActiveScaffold::Core

Defined in:
lib/active_scaffold/core.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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 = '::')
  error_message = []
  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.message["uninitialized constant #{controller}"]

        error_message << "#{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 #{error_message.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_configObject



12
13
14
15
16
17
# File 'lib/active_scaffold/core.rb', line 12

def active_scaffold_config
  @active_scaffold_config ||= begin
     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_settingsObject



7
8
9
10
# File 'lib/active_scaffold/core.rb', line 7

def 
  config = self.class.active_scaffold_config
  config.(, params)
end

#user_settings_storageObject



29
30
31
32
33
34
35
# File 'lib/active_scaffold/core.rb', line 29

def 
  if self.class.active_scaffold_config.
    active_scaffold_session_storage
  else
    {}
  end
end