Class: Lanes::API::Controller
- Inherits:
-
Object
- Object
- Lanes::API::Controller
- Defined in:
- lib/lanes/api/controller.rb
Overview
The Controller handles querying models using either pre-defined scopes or hash based queries; and also including optional associations with the reply
It assigns the following meaning the these parameters.
* f: (fields) Include the following fields (usually methods) with the reply
* w: (with) Uses the defined scope to query and/or add extra data to the model
* q: (query) Query the model using fields and values
it is an array of clauses, which can be either forms
{ field: value }, or { field: { op: 'like', value: 'value%' } }
* i: (include) Include associations along with the model in the reply
* o: (order) Order by, { field => "ASC|DESC" }
* l: (limit) Limit the returned rows to the count
* s: (start) Start the query at the given offset (for paging)
* df: (data format) Should data be returned as 'object' (default) or 'array'
The parameters are deliberately shortened so they can be used in query parameters without blowing the URL up to an unacceptable length
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Instance Method Summary collapse
-
#initialize(model, authentication, params, data = {}) ⇒ Controller
constructor
A new instance of Controller.
- #perform_creation ⇒ Object
- #perform_destroy ⇒ Object
- #perform_retrieval ⇒ Object
- #perform_update ⇒ Object
Constructor Details
#initialize(model, authentication, params, data = {}) ⇒ Controller
Returns a new instance of Controller.
26 27 28 29 30 31 |
# File 'lib/lanes/api/controller.rb', line 26 def initialize(model, authentication, params, data={}) @user = authentication.current_user @model = model @params = params @data = data end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
24 25 26 |
# File 'lib/lanes/api/controller.rb', line 24 def data @data end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
24 25 26 |
# File 'lib/lanes/api/controller.rb', line 24 def model @model end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
24 25 26 |
# File 'lib/lanes/api/controller.rb', line 24 def params @params end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
24 25 26 |
# File 'lib/lanes/api/controller.rb', line 24 def user @user end |
Instance Method Details
#perform_creation ⇒ Object
45 46 47 48 49 |
# File 'lib/lanes/api/controller.rb', line 45 def perform_creation record = model.from_attribute_data(data, user) = .merge(success: record.save) build_reply(record, :create, ) end |
#perform_destroy ⇒ Object
59 60 61 62 63 64 65 |
# File 'lib/lanes/api/controller.rb', line 59 def perform_destroy if params[:id] perform_single_destroy elsif data.is_a?(Array) perform_multiple_destroy end end |
#perform_retrieval ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/lanes/api/controller.rb', line 33 def perform_retrieval query = build_query = [:total_count] = query.dup.count if should_include_total_count? query = add_modifiers_to_query(query) Lanes.logger.warn "ID: #{params[:id]}" if params[:id] query = query.first! end build_reply(query, :retrieve, ) end |
#perform_update ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/lanes/api/controller.rb', line 51 def perform_update if params[:id] perform_single_update( build_query.first! ) elsif data.is_a?(Array) perform_multiple_updates end end |