module LoadModules

Public Instance Methods

start_controller_load() click to toggle source
# File lib/expandable/load_modules.rb, line 6
def start_controller_load
  load_modules('app/controllers/expandables', false)
end
start_model_load() click to toggle source
# File lib/expandable/load_modules.rb, line 2
def start_model_load
  load_modules('app/models/expandables')
end

Private Instance Methods

load_expandables(target_class, target_module, is_extend) click to toggle source
# File lib/expandable/load_modules.rb, line 61
def load_expandables(target_class, target_module, is_extend)
  if (target_class.class == Class) && (target_module.class == Module)
    ActiveSupport::Dependencies.explicitly_unloadable_constants << target_module

    if is_extend
      if Rails.env.development?
        puts '    Loading class module: ' + target_module.to_s
      end
      target_class.send :extend, target_module
    else
      if Rails.env.development?
        puts '    Loading instance module: ' + target_module.to_s
      end
      target_class.send :include, target_module
    end
  end
end
load_modules(path, is_model = true) click to toggle source
# File lib/expandable/load_modules.rb, line 11
def load_modules(path, is_model = true)
  return unless File.exist?(path)
  expandables = Dir["#{path}/**/*.rb"]

  unless expandables.empty?
    expandables.each do |modu|
      if Rails.env.development?
        puts ''
        puts '  Found expandable file: ' + modu
      end

      modu = File.basename(modu).split('.')[0..-2].join
      parts = modu.split('_')

      if parts.length > 1
        if is_model
          target_class = parts[-1]
                           .singularize
                           .capitalize
                           .classify
                           .safe_constantize

          if Rails.env.development?
            puts '    Target Model: ' + target_class.to_s
            puts '    Target Object Type: ' + target_class.class.name
          end
        else
          target_class = [parts[-2].pluralize, parts[-1]].join('_')
                           .capitalize
                           .classify
                           .safe_constantize

          if Rails.env.development?
            puts '    Target Controller: ' + target_class.to_s
            puts '    Target Object Type: ' + target_class.class.name
          end
        end

        expand_module = modu.camelize.classify.safe_constantize
        load_expandables(target_class, expand_module, parts[0].downcase == 'self')
      else
        if Rails.env.development?
          puts '    The file was not named properly: ' + modu
        end
        next
      end
    end
  end
end