Class: Happy::Extras::ActiveModelResourceController

Inherits:
ResourceController show all
Defined in:
lib/happy/extras/active_model_resource_controller.rb

Constant Summary

Constants inherited from Controller

Controller::CASCADING_SETTINGS

Instance Attribute Summary

Attributes inherited from Controller

#env, #processed_path, #unprocessed_path

Attributes included from Helpers::Rendering

#output_buffer

Instance Method Summary collapse

Methods inherited from Controller

#app, #current_url, helpers, #initialize, #params, #request, #response, #session

Methods included from Helpers::I18n

#localize, #translate

Methods included from Helpers::Rendering

#capture_template_block, #concat_output, #render, #render_resource, #render_template, #with_output_buffer

Methods included from Helpers::Html

#escape_html, #html_tag, #html_tag_attributes, #link_to, #preserve, #url_for

Methods included from Controller::Permissions

#permissions

Methods included from Controller::Configurable

#set, #settings

Methods included from Controller::Rackable

#handle_request

Methods included from Controller::Actions

#cache_control, #content_type, #halt!, #header, #layout, #max_age, #only_if_path_matches, #redirect!, #run, #serve!

Methods included from Controller::Routing

#on, #path_to_regexp

Constructor Details

This class inherits a constructor from Happy::Controller

Instance Method Details

#createObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/happy/extras/active_model_resource_controller.rb', line 61

def create
  require_permission! :create
  set_singular_variable resource_with_permission_scope(:create).new(params[settings[:singular_name]], :as => settings[:role])

  if singular_variable.save
    redirect! singular_variable
  else
    render_resource_template 'new'
  end
end

#editObject



72
73
74
75
76
# File 'lib/happy/extras/active_model_resource_controller.rb', line 72

def edit
  require_permission! :edit
  set_singular_variable resource_with_permission_scope(:edit).find(params['id'])
  render_resource_template 'edit'
end

#indexObject



43
44
45
46
47
# File 'lib/happy/extras/active_model_resource_controller.rb', line 43

def index
  require_permission! :index
  set_plural_variable resource_with_permission_scope(:index).all
  render_resource_template 'index'
end

#newObject



55
56
57
58
59
# File 'lib/happy/extras/active_model_resource_controller.rb', line 55

def new
  require_permission! :new
  set_singular_variable resource_with_permission_scope(:new).new(params[settings[:singular_name]], :as => settings[:role])
  render_resource_template 'new'
end

#plural_variableObject



31
32
33
# File 'lib/happy/extras/active_model_resource_controller.rb', line 31

def plural_variable
  instance_variable_get "@#{settings[:plural_name]}"
end

#render_resource_template(name) ⇒ Object



11
12
13
# File 'lib/happy/extras/active_model_resource_controller.rb', line 11

def render_resource_template(name)
  render "#{settings[:plural_name]}/#{name}.html.haml"
end

#require_permission!(*args) ⇒ Object



23
24
25
# File 'lib/happy/extras/active_model_resource_controller.rb', line 23

def require_permission!(*args)
  raise "not allowed" unless permissions.can?(*args, settings[:class])
end

#resourceObject



15
16
17
# File 'lib/happy/extras/active_model_resource_controller.rb', line 15

def resource
  settings[:class]
end

#resource_with_permission_scope(*args) ⇒ Object



19
20
21
# File 'lib/happy/extras/active_model_resource_controller.rb', line 19

def resource_with_permission_scope(*args)
  permissions.scoped_model(*args, settings[:class])
end

#root_urlObject



7
8
9
# File 'lib/happy/extras/active_model_resource_controller.rb', line 7

def root_url
  super(settings[:plural_name])
end

#routeObject



90
91
92
93
94
95
# File 'lib/happy/extras/active_model_resource_controller.rb', line 90

def route
  settings[:singular_name] ||= settings[:class].to_s.tableize.singularize
  settings[:plural_name]   ||= settings[:class].to_s.tableize.pluralize

  super
end

#set_plural_variable(v) ⇒ Object



27
28
29
# File 'lib/happy/extras/active_model_resource_controller.rb', line 27

def set_plural_variable(v)
  instance_variable_set "@#{settings[:plural_name]}", v
end

#set_singular_variable(v) ⇒ Object



35
36
37
# File 'lib/happy/extras/active_model_resource_controller.rb', line 35

def set_singular_variable(v)
  instance_variable_set "@#{settings[:singular_name]}", v
end

#showObject



49
50
51
52
53
# File 'lib/happy/extras/active_model_resource_controller.rb', line 49

def show
  require_permission! :show
  set_singular_variable resource_with_permission_scope(:show).find(params['id'])
  render_resource_template 'show'
end

#singular_variableObject



39
40
41
# File 'lib/happy/extras/active_model_resource_controller.rb', line 39

def singular_variable
  instance_variable_get "@#{settings[:singular_name]}"
end

#updateObject



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/happy/extras/active_model_resource_controller.rb', line 78

def update
  require_permission! :update
  set_singular_variable resource_with_permission_scope(:update).find(params['id'])
  singular_variable.assign_attributes params[settings[:singular_name]], :as => settings[:role]

  if singular_variable.save
    redirect! singular_variable
  else
    render_resource_template 'edit'
  end
end