Module: ApiResource::ClassMethods

Defined in:
lib/ocean/api_resource.rb

Instance Method Summary collapse

Instance Method Details

#collection(bag = {}) ⇒ Object

This method implements the common behaviour in Ocean for requesting collections of resources, including conditions, GROUP and substring searches. It can be used directly on a class:

@collection = ApiUser.collection(params)

or on any Relation:

@collection = @api_user.groups.collection(params)

Since a Relation is returned, further chaining is possible:

@collection = @api_user.groups.collection(params).active.order("email ASC")

The whole params hash can safely be passed as the input arg: keys are filtered so that matches only are done against the attributes declared in the controller using ocean_resource_model. Ranges are allowed for those attributes declared to accept them using the ranged parameter of ocean_resource_model.

The group: keyword arg, if present, adds a GROUP clause to the generated SQL.

The search: keyword arg, if present, searches for the value in the database string or text column declared in the controller’s ocean_resource_model declaration. The search is done using an SQL LIKE clause, with the substring framed by wildcard characters. It’s self-evident that this is not an efficient search method for larger datasets; in such cases, other search methods should be employed.

If page: is present, pagination will be added. If page is less than zero, an empty Relation will be returned. Otherwise, page_size: (default 25) will be used to calculate OFFSET and LIMIT. The default page_size for a resource class can also be declared using ocean_resource_model.

Right restrictions, if provided, will be added to the relation before it is returned.



46
47
48
49
# File 'lib/ocean/api_resource.rb', line 46

def collection(bag={})
  collection_internal bag, bag[:group], bag[:search], bag[:page], bag[:page_size],
                      bag['_right_restrictions']
end

#invalidateObject

Invalidate all members of this class in Varnish using a BAN requests to the caches in the Ocean environment. The BAN requests are done in parallel. The number of BAN requests, and the exact URI composition in each request, is determined by the invalidate_collection: arg to the ocean_resource_model declaration in the model.



129
130
131
132
133
134
# File 'lib/ocean/api_resource.rb', line 129

def invalidate
  resource_name = name.pluralize.underscore
  varnish_invalidate_collection.each do |suffix|
    Api.ban "/v[0-9]+/#{resource_name}#{suffix}"
  end
end

#latest_api_versionObject

Returns the latest version for the resource class. E.g.:

> ApiUser.latest_version
"v1"


118
119
120
# File 'lib/ocean/api_resource.rb', line 118

def latest_api_version
  Api.version_for(self.class.name.pluralize.underscore)
end