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



232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/active_scaffold/core.rb', line 232

def self.active_record_column_type_cast(value, column)
  if Rails.version < '4.2'
    column.type_cast value
  elsif 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 = ActiveModel::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.



192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/active_scaffold/core.rb', line 192

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
        if error.message["uninitialized constant #{controller}"]
          error_message << "#{namespace}#{controller_name.camelize}Controller"
          next
        else
          raise
        end
      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



218
219
220
221
222
223
224
225
226
# File 'lib/active_scaffold/core.rb', line 218

def self.column_type_cast(value, column)
  if defined?(ActiveRecord) && ActiveRecord::ConnectionAdapters::Column === column
    active_record_column_type_cast(value, column)
  elsif defined?(Mongoid) && Mongoid::Fields::Standard === column
    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



228
229
230
# File 'lib/active_scaffold/core.rb', line 228

def self.mongoid_column_type_cast(value, column)
  column.type.evolve value
end

Instance Method Details

#active_scaffold_configObject



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

def active_scaffold_config
  self.class.active_scaffold_config
end

#active_scaffold_config_for(klass) ⇒ Object



11
12
13
# File 'lib/active_scaffold/core.rb', line 11

def active_scaffold_config_for(klass)
  self.class.active_scaffold_config_for(klass)
end