Module: Axel::ControllerHelpers

Extended by:
ActiveSupport::Concern
Included in:
BaseController
Defined in:
lib/axel/controller_helpers.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#drop_meta!Object



176
177
178
# File 'lib/axel/controller_helpers.rb', line 176

def drop_meta!
  .drop!
end

#drop_meta?Boolean

Returns:

  • (Boolean)


180
181
182
# File 'lib/axel/controller_helpers.rb', line 180

def drop_meta?
  .drop?
end

#errorsObject

Public: Quick access to a set of errors we’re tracking while dealing with a request

Example:

errors
# => <# Errors ...>

Return an object for recording errors over a request



73
74
75
# File 'lib/axel/controller_helpers.rb', line 73

def errors
  @errors ||= Payload::Errors.new
end

#find_resource(options = {}) ⇒ Object

Public: Use as a before filter. Will find a resource with some automation. Find based on params.

options - Hash of options for tweaking

:finder   - Column being used for the select (:id, :user_name)
:value    - The value the column should be (an ID or Name value)

Example:

PersonasController#find_resource # (with params[:id] => 1)
# => @persona # => <# Persona id: 1 #>

PersonasController#find_resource(finder: :user_id) # (with params[:user_id] => 1)
# => @persona # => <# Persona user_id: 1 #>

PersonasController#find_resource(finder: :user_id, value: 2) # (with params[:user_id] => 1)
# => @persona # => <# Persona user_id: 2 #>

Return the value of the instance variable we just set



136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/axel/controller_helpers.rb', line 136

def find_resource(options = {})
  resource_name = controller_name.singularize
  resource = controller_name.classify.constantize
  finder_column = options[:finder] || :id
  finder_value = options[:value] || params[:id]
  resources = resource.where(finder_column => finder_value)
  if resources.length == 0
    raise RecordNotFound
  else
    instance_variable_set "@#{resource_name}", resources.first
  end
end

#force_ssl!Object



169
170
171
172
173
174
# File 'lib/axel/controller_helpers.rb', line 169

def force_ssl!
  return true unless Rails::Application.productionish?
  if !request.ssl?
    raise ForceSSL
  end
end

#formatObject



184
185
186
# File 'lib/axel/controller_helpers.rb', line 184

def format
  params[:format] || :json
end

#metadataObject



77
78
79
# File 'lib/axel/controller_helpers.rb', line 77

def 
  @metadata ||= Payload::Metadata.new
end

#object_nameObject



165
166
167
# File 'lib/axel/controller_helpers.rb', line 165

def object_name
  controller_name.singularize
end

#object_paramsObject



157
158
159
# File 'lib/axel/controller_helpers.rb', line 157

def object_params
  try_strong_params post_params.fetch(object_name, post_params)
end

#post_paramsObject



153
154
155
# File 'lib/axel/controller_helpers.rb', line 153

def post_params
  try_strong_params request.POST
end

#query_paramsObject



149
150
151
# File 'lib/axel/controller_helpers.rb', line 149

def query_params
  try_strong_params request.GET
end

#rabl_render(object, view) ⇒ Object



195
196
197
# File 'lib/axel/controller_helpers.rb', line 195

def rabl_render(object, view)
  Rabl.render(object, view, view_path: 'app/views', scope: self)
end

#render_nil_formatObject



188
189
190
191
192
193
# File 'lib/axel/controller_helpers.rb', line 188

def render_nil_format
  {
    json: nil,
    xml: ""
  }.with_indifferent_access[format]
end

#rescue_error(options = {}) ⇒ Object

Default API response when an error occurs



82
83
84
85
86
# File 'lib/axel/controller_helpers.rb', line 82

def rescue_error(options = {})
  messages = ([options[:messages]] + [options[:message]]).flatten.compact
  errors.new_error options[:status], *messages
  respond_to_empty
end

#respond_to_emptyObject Also known as: render_empty

Public: Use to respond with an empty template. Especially useful for manipulative controller actions where you don’t want to have a new view, but you DO want to return our “envelope”



109
110
111
112
113
114
# File 'lib/axel/controller_helpers.rb', line 109

def respond_to_empty
  respond_to do |f|
    f.json { render nothing: true, layout: "axel", status: header_status }
    f.xml { render nothing: true, layout: "axel", status: header_status }
  end
end

#respond_with_action(action) ⇒ Object Also known as: render_action

Public: Use for weird actions like #update where we don’t want to create a whole rabl template for it. This will allow the format to fixed correctly and render a different action for you.

action - used to pick the template we’re now rendering

Example:

respond_with_action :show


98
99
100
101
102
103
# File 'lib/axel/controller_helpers.rb', line 98

def respond_with_action(action)
  respond_with do |f|
    f.json { render action: action }
    f.xml { render action: action }
  end
end

#safe_json_load(json) ⇒ Object



199
200
201
# File 'lib/axel/controller_helpers.rb', line 199

def safe_json_load(json)
  json.present? ? MultiJson.load(json, mode: :null) : nil
end

#try_strong_params(regular_params) ⇒ Object



161
162
163
# File 'lib/axel/controller_helpers.rb', line 161

def try_strong_params(regular_params)
  ControllerParameters.new(regular_params).params_object
end

#xml_clean(payload) ⇒ Object



203
204
205
206
207
# File 'lib/axel/controller_helpers.rb', line 203

def xml_clean(payload)
  payload.gsub(/\<(\/)*hash\>\s{1}/, '').
    gsub(/<\?\W*([xX][mM][lL])\W*version.*encoding.*\?>/, "").
    strip
end