Class: AdminAssistant::Request::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/admin_assistant/request/base.rb

Instance Method Summary collapse

Constructor Details

#initialize(admin_assistant, controller) ⇒ Base

Returns a new instance of Base.



16
17
18
19
# File 'lib/admin_assistant/request/base.rb', line 16

def initialize(admin_assistant, controller)
  @admin_assistant, @controller = admin_assistant, controller
  controller.instance_variable_set :@admin_assistant, admin_assistant
end

Instance Method Details

#actionObject



21
22
23
# File 'lib/admin_assistant/request/base.rb', line 21

def action
  @controller.action_name
end

#after_template_file(template_name) ⇒ Object



25
26
27
28
29
30
# File 'lib/admin_assistant/request/base.rb', line 25

def after_template_file(template_name)
  File.join(
    Rails.root, 'app/views/', @controller.controller_path, 
    "_after_#{template_name}.html.erb"
  )
end

#before_template_file(template_name) ⇒ Object



32
33
34
35
36
37
# File 'lib/admin_assistant/request/base.rb', line 32

def before_template_file(template_name)
  File.join(
    Rails.root, 'app/views/', @controller.controller_path, 
    "_before_#{template_name}.html.erb"
  )
end

#model_classObject



39
40
41
# File 'lib/admin_assistant/request/base.rb', line 39

def model_class
  @admin_assistant.model_class
end

#model_class_symbolObject



43
44
45
# File 'lib/admin_assistant/request/base.rb', line 43

def model_class_symbol
  model_class.name.underscore.to_sym
end

#originObject



47
48
49
50
# File 'lib/admin_assistant/request/base.rb', line 47

def origin
  @controller.params[:origin] || @controller.request.referer ||
      {:action => 'index'}
end

#render_multi_form(records) ⇒ Object



58
59
60
61
62
# File 'lib/admin_assistant/request/base.rb', line 58

def render_multi_form(records)
  @controller.instance_variable_set :@records, records
  @controller.instance_variable_set :@origin, origin
  render_template_file 'multi_form'
end

#render_single_form(record) ⇒ Object



52
53
54
55
56
# File 'lib/admin_assistant/request/base.rb', line 52

def render_single_form(record)
  @controller.instance_variable_set :@record, record
  @controller.instance_variable_set :@origin, origin
  render_template_file 'form'
end

#render_template_file(template_name = action, opts_plus = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/admin_assistant/request/base.rb', line 64

def render_template_file(template_name = action, opts_plus = {})
  html = ''
  html << render_to_string_if_exists(
    before_template_file(template_name), opts_plus
  )
  html << render_to_string(
    AdminAssistant.template_file(template_name), opts_plus
  )
  html << render_to_string_if_exists(
    after_template_file(template_name), opts_plus
  )
  render_as_text_opts = {:text => html, :layout => true}.merge(opts_plus)
  @controller.send :render, render_as_text_opts
end

#render_to_string(template_file, options_plus) ⇒ Object



79
80
81
82
83
84
# File 'lib/admin_assistant/request/base.rb', line 79

def render_to_string(template_file, options_plus)
  after_template_opts = {
    :file => template_file, :layout => false
  }.merge options_plus
  @controller.send :render_to_string, after_template_opts
end

#render_to_string_if_exists(template_file, opts_plus) ⇒ Object



86
87
88
89
90
91
92
# File 'lib/admin_assistant/request/base.rb', line 86

def render_to_string_if_exists(template_file, opts_plus)
  if File.exist?(template_file)
    render_to_string(template_file, opts_plus)
  else
    ''
  end
end