module ActiveScaffold::ClassMethods

Public Instance Methods

active_scaffold(model_id = nil, &block) click to toggle source
# File lib/active_scaffold.rb, line 164
def active_scaffold(model_id = nil, &block)
  # initialize bridges here
  ActiveScaffold::Bridges.run_all

  # converts Foo::BarController to 'bar' and FooBarsController to 'foo_bar' and AddressController to 'address'
  model_id = self.to_s.split('::').last.sub(%rController$/, '').pluralize.singularize.underscore unless model_id

  # run the configuration
  @active_scaffold_config = ActiveScaffold::Config::Core.new(model_id)
  @active_scaffold_config_block = block
  self.links_for_associations

  @active_scaffold_frontends = []
  if active_scaffold_config.frontend.to_sym != :default
    active_scaffold_custom_frontend_path = File.join(ActiveScaffold::Config::Core.plugin_directory, 'frontends', active_scaffold_config.frontend.to_s , 'views')
    @active_scaffold_frontends << active_scaffold_custom_frontend_path
  end
  active_scaffold_default_frontend_path = File.join(ActiveScaffold::Config::Core.plugin_directory, 'frontends', 'default' , 'views')
  @active_scaffold_frontends << active_scaffold_default_frontend_path
  @active_scaffold_custom_paths = []

  self.active_scaffold_superclasses_blocks.each {|superblock| self.active_scaffold_config.configure &superblock}
  self.active_scaffold_config.sti_children = nil # reset sti_children if set in parent block
  self.active_scaffold_config.configure &block if block_given?
  self.active_scaffold_config._configure_sti unless self.active_scaffold_config.sti_children.nil?
  self.active_scaffold_config._load_action_columns

  # defines the attribute read methods on the model, so record.send() doesn't find protected/private methods instead
  klass = self.active_scaffold_config.model
  klass.define_attribute_methods unless klass.attribute_methods_generated?
  # include the rest of the code into the controller: the action core and the included actions
  module_eval do
    include ActiveScaffold::Finder
    include ActiveScaffold::Constraints
    include ActiveScaffold::AttributeParams
    include ActiveScaffold::Actions::Core
    active_scaffold_config.actions.each do |mod|
      name = mod.to_s.camelize
      include "ActiveScaffold::Actions::#{name}".constantize

      # sneak the action links from the actions into the main set
      if link = active_scaffold_config.send(mod).link rescue nil
        if link.is_a? Array
          link.each {|current| active_scaffold_config.action_links.add_to_group(current, active_scaffold_config.send(mod).action_group)}
        elsif link.is_a? ActiveScaffold::DataStructures::ActionLink
          active_scaffold_config.action_links.add_to_group(link, active_scaffold_config.send(mod).action_group)
        end
      end
    end
  end
  self.append_view_path active_scaffold_paths
  self._add_sti_create_links if self.active_scaffold_config.add_sti_create_links?
end
active_scaffold_config() click to toggle source
# File lib/active_scaffold.rb, line 306
def active_scaffold_config
  if @active_scaffold_config.nil?
    self.superclass.active_scaffold_config if self.superclass.respond_to? :active_scaffold_config
  else
    @active_scaffold_config
  end
end
active_scaffold_config_block() click to toggle source
# File lib/active_scaffold.rb, line 314
def active_scaffold_config_block
  @active_scaffold_config_block
end
active_scaffold_config_for(klass) click to toggle source
# File lib/active_scaffold.rb, line 328
def active_scaffold_config_for(klass)
  begin
    controller = active_scaffold_controller_for(klass)
  rescue ActiveScaffold::ControllerNotFound
    config = ActiveScaffold::Config::Core.new(klass)
    config._load_action_columns
    config
  else
    controller.active_scaffold_config
  end
end
active_scaffold_controller_for(klass) click to toggle source

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 “#{model}Controller” syntax. You may override this method to customize the search routine.

# File lib/active_scaffold.rb, line 343
def active_scaffold_controller_for(klass)
  controller_namespace = self.to_s.split('::')[0...-1].join('::') + '::'
  error_message = []
  [controller_namespace, ''].each do |namespace|
    ["#{klass.to_s.underscore.pluralize}", "#{klass.to_s.underscore.pluralize.singularize}"].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?
      raise ActiveScaffold::ControllerNotFound, "ActiveScaffold on #{controller} is not for #{klass} model.", caller unless controller.active_scaffold_config.model.to_s == klass.to_s
      return controller
    end
  end
  raise ActiveScaffold::ControllerNotFound, "Could not find " + error_message.join(" or "), caller
end
active_scaffold_controller_for_column(column, options = {}) click to toggle source
# File lib/active_scaffold.rb, line 249
def active_scaffold_controller_for_column(column, options = {})
  begin
    if column.polymorphic_association?
      :polymorph
    elsif options.include?(:controller)
      "#{options[:controller].to_s.camelize}Controller".constantize
    else
      active_scaffold_controller_for(column.association.klass)
    end
  rescue ActiveScaffold::ControllerNotFound
    nil        
  end
end
active_scaffold_paths() click to toggle source
# File lib/active_scaffold.rb, line 297
def active_scaffold_paths
  return @active_scaffold_paths unless @active_scaffold_paths.nil?

  @active_scaffold_paths = []
  @active_scaffold_paths.concat @active_scaffold_custom_paths unless @active_scaffold_custom_paths.nil?
  @active_scaffold_paths.concat @active_scaffold_frontends unless @active_scaffold_frontends.nil?
  @active_scaffold_paths
end
active_scaffold_superclasses_blocks() click to toggle source
# File lib/active_scaffold.rb, line 318
def active_scaffold_superclasses_blocks
  blocks = []
  klass = self.superclass
  while klass.respond_to? :active_scaffold_superclasses_blocks
    blocks << klass.active_scaffold_config_block
    klass = klass.superclass
  end
  blocks.compact.reverse
end
add_active_scaffold_path(path) click to toggle source
# File lib/active_scaffold.rb, line 292
def add_active_scaffold_path(path)
  @active_scaffold_paths = nil # Force active_scaffold_paths to rebuild
  @active_scaffold_custom_paths << path
end
parent_prefixes() click to toggle source
# File lib/active_scaffold.rb, line 218
def parent_prefixes
  @parent_prefixes ||= super << 'active_scaffold_overrides' << ''
end
uses_active_scaffold?() click to toggle source
# File lib/active_scaffold.rb, line 367
def uses_active_scaffold?
  !active_scaffold_config.nil?
end