Module: ResourceController::Controller

Defined in:
lib/resource_controller/controller.rb

Class Method Summary collapse

Class Method Details

.included(subclass) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/resource_controller/controller.rb', line 3

def self.included(subclass)
  subclass.class_eval do
    include ResourceController::Helpers
    include ResourceController::Actions
    extend  ResourceController::Accessors
    extend  ResourceController::ClassMethods
    
    class_reader_writer :belongs_to, *NAME_ACCESSORS
    NAME_ACCESSORS.each { |accessor| send(accessor, controller_name.singularize.underscore) }

    ACTIONS.each do |action|
      class_scoping_reader action, FAILABLE_ACTIONS.include?(action) ? ResourceController::FailableActionOptions.new : ResourceController::ActionOptions.new
    end

    self.helper_method :object_url, :edit_object_url, :new_object_url, :collection_url, :object, :collection, 
                         :parent, :parent_type, :parent_object, :parent_model, :model_name, :model, :object_path, 
                         :edit_object_path, :new_object_path, :collection_path, :hash_for_collection_path, :hash_for_object_path, 
                            :hash_for_edit_object_path, :hash_for_new_object_path, :hash_for_collection_url, 
                              :hash_for_object_url, :hash_for_edit_object_url, :hash_for_new_object_url, :parent?,
                                :collection_url_options, :object_url_options, :new_object_url_options
                            
  end
  
  init_default_actions(subclass)
end

.init_default_actions(klass) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/resource_controller/controller.rb', line 30

def self.init_default_actions(klass)
  klass.class_eval do
    index.wants.html
    edit.wants.html
    new_action.wants.html

    show do
      wants.html

      failure.wants.html { render :text => "Member object not found." }
    end

    create do
      flash "Successfully created!"
      wants.html { redirect_to object_url }

      failure.wants.html { render :action => "new" }
    end

    update do
      flash "Successfully updated!"
      wants.html { redirect_to object_url }

      failure.wants.html { render :action => "edit" }
    end

    destroy do
      flash "Successfully removed!"
      wants.html { redirect_to collection_url }
      failure.wants.html { redirect_to object_url }
    end
    
    class << self
      def singleton?
        false
      end
    end
  end
end