Module: Homeschool::ControllerExtensions::ClassMethods

Defined in:
lib/controller_extensions.rb

Overview

Add find_object, model_name

Instance Method Summary collapse

Instance Method Details

#find_by_id(klass, options = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/controller_extensions.rb', line 8

def find_by_id(klass, options={})
  options = {:name => klass.name.underscore, :param => :id}.merge(options)
  before_filter options.filter(:only, :except) do |controller|
    controller.send(:instance_variable_set, options[:name].to_sym.iv, klass.find(controller.params[options[:param]]))
  end
end

#find_object(options = {}) ⇒ Object



4
5
6
# File 'lib/controller_extensions.rb', line 4

def find_object(options={})
  find_by_id(model_name, {:only => [:update, :edit, :show, :destroy]}.merge(options))
end

#model_classObject



19
20
21
# File 'lib/controller_extensions.rb', line 19

def model_class
  model_name.classify.constantize
end

#model_nameObject



15
16
17
# File 'lib/controller_extensions.rb', line 15

def model_name
  name.sub(/Controller$/, '').singularize.underscore
end