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) ⇒ Object



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/active_scaffold/core.rb', line 272

def self.active_record_column_type_cast(value, column)
  return Time.zone.at(value.to_i) if value =~ /\A\d+\z/ && %i[time datetime].include?(column.type)
  if Rails.version < '5.0'
    column.type_cast_from_user value
  elsif column.type.respond_to? :cast # jruby-jdbc and rails 5
    column.type.cast value
  else
    cast_type = ActiveRecord::Type.lookup column.type
    cast_type ? cast_type.cast(value) : value
  end
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.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/active_scaffold/core.rb', line 234

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 => error
        # Only rescue NameError associated with the controller constant not existing - not other compile errors
        raise unless error.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



257
258
259
260
261
262
263
264
265
# File 'lib/active_scaffold/core.rb', line 257

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?(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



267
268
269
270
# File 'lib/active_scaffold/core.rb', line 267

def self.mongoid_column_type_cast(value, column)
  return Time.zone.at(value.to_i) if value =~ /\A\d+\z/ && [Time, DateTime].include?(column.type)
  column.type.evolve value
end

Instance Method Details

#active_scaffold_configObject



17
18
19
20
21
22
23
24
25
26
# File 'lib/active_scaffold/core.rb', line 17

def active_scaffold_config
  @active_scaffold_config ||= begin
     unless self.class.active_scaffold_config.user
    if ActiveScaffold.threadsafe
      self.class.active_scaffold_config.user
    else
      self.class.active_scaffold_config
    end
  end
end

#active_scaffold_session_storage(id = nil) ⇒ Object



33
34
35
36
37
# File 'lib/active_scaffold/core.rb', line 33

def active_scaffold_session_storage(id = nil)
  session_index = active_scaffold_session_storage_key(id)
  session[session_index] ||= {}
  session[session_index]
end

#active_scaffold_session_storage_key(id = nil) ⇒ Object



28
29
30
31
# File 'lib/active_scaffold/core.rb', line 28

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
11
12
13
14
15
# File 'lib/active_scaffold/core.rb', line 7

def 
  config = self.class.active_scaffold_config
  config.(, params)
  return if ActiveScaffold.threadsafe
  config.actions.each do |action_name|
    conf_instance = config.send(action_name) rescue next # rubocop:disable Style/RescueModifier
    config.user.(conf_instance)
  end
end

#user_settings_storageObject



39
40
41
42
43
44
45
# File 'lib/active_scaffold/core.rb', line 39

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