Class: Freddie::Handlers::Resource

Inherits:
Base
  • Object
show all
Defined in:
lib/freddie/handlers.rb

Instance Attribute Summary

Attributes inherited from Base

#args, #block, #env

Instance Method Summary collapse

Methods inherited from Base

#after_perform, #before_perform, #call, call, #context, #finish!, #finished?, #initialize, #layout, #method_missing, #params, #perform_freddie_block, #request, #response, #session

Constructor Details

This class inherits a constructor from Freddie::Handlers::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Freddie::Handlers::Base

Instance Method Details

#performObject



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/freddie/handlers.rb', line 144

def perform
  name = args.shift
  options = args.shift || {}

  options = {
    name: name.to_s,
    singular_name: name.to_s.singularize,
    plural_name: name.to_s.pluralize,
    klass: name.to_s.classify.constantize
  }.merge(options)

  path(options[:name]) do |n|
    n.path('new') do
      n.template "#{options[:plural_name]}/new.html.haml",
        options[:singular_name] => options[:klass].new
    end

    n.path(:id) do |n|
      resource = options[:klass].find(params[:id])

      n.path('edit') do |n|
        n.template "#{options[:plural_name]}/edit.html.haml",
          options[:singular_name] => resource
      end

      n.template "#{options[:plural_name]}/show.html.haml",
        options[:singular_name] => resource
    end

    n.template "#{options[:plural_name]}/index.html.haml",
      options[:plural_name] => options[:klass].all
  end
end