Class: Lanes::API::Controller

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#dataObject (readonly)

Returns the value of attribute data.



24
25
26
# File 'lib/lanes/api/controller.rb', line 24

def data
  @data
end

#modelObject (readonly)

Returns the value of attribute model.



24
25
26
# File 'lib/lanes/api/controller.rb', line 24

def model
  @model
end

#paramsObject (readonly)

Returns the value of attribute params.



24
25
26
# File 'lib/lanes/api/controller.rb', line 24

def params
  @params
end

#userObject (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_creationObject



45
46
47
48
49
# File 'lib/lanes/api/controller.rb', line 45

def perform_creation
    record  = model.from_attribute_data(data, user)
    options = build_reply_options.merge(success: record.save)
    build_reply(record, :create, options)
end

#perform_destroyObject



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_retrievalObject



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
    options = build_reply_options
    options[: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, options)
end

#perform_updateObject



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