Class: Trestle::Adapters::Adapter

Inherits:
Object
  • Object
show all
Includes:
EvaluationContext
Defined in:
lib/trestle/adapters/adapter.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin, context = nil) ⇒ Adapter

Returns a new instance of Adapter.


9
10
11
12
# File 'lib/trestle/adapters/adapter.rb', line 9

def initialize(admin, context=nil)
  @admin = admin
  @context = context
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Trestle::EvaluationContext

Instance Attribute Details

#adminObject (readonly)

Returns the value of attribute admin


6
7
8
# File 'lib/trestle/adapters/adapter.rb', line 6

def admin
  @admin
end

Instance Method Details

#build_instance(attrs = {}, params = {}) ⇒ Object

Builds (and returns) a new instance for new/create actions.

attrs - Permitted attributes to set on the new instance params - Unfiltered params hash from the controller

Raises:

  • (NotImplementedError)

34
35
36
# File 'lib/trestle/adapters/adapter.rb', line 34

def build_instance(attrs={}, params={})
  raise NotImplementedError
end

#collection(params = {}) ⇒ Object

Loads the initial collection for use by the index action.

params - Unfiltered params hash from the controller

Returns a scope object that can be chained with other methods (e.g. sort, paginate, count, etc).

Raises:

  • (NotImplementedError)

19
20
21
# File 'lib/trestle/adapters/adapter.rb', line 19

def collection(params={})
  raise NotImplementedError
end

#count(collection) ⇒ Object

Counts the number of objects in a collection for use by scope links.

collection - The collection to count

Returns the total number (integer) of objects in the collection.

Raises:

  • (NotImplementedError)

117
118
119
# File 'lib/trestle/adapters/adapter.rb', line 117

def count(collection)
  raise NotImplementedError
end

#decorate_collection(collection) ⇒ Object

Decorates a collection for rendering by the index view. Decorating is the final step in preparing the collection for the view.

collection - The collection to decorate

Returns an enumerable collection of instances.


87
88
89
# File 'lib/trestle/adapters/adapter.rb', line 87

def decorate_collection(collection)
  collection
end

#default_form_attributesObject

Generates a list of attributes that should be rendered by the new/show/edit (form) views.

Returns an Array of Trestle::Attribute and/or Trestle::Attribute::Association objects.

Raises:

  • (NotImplementedError)

181
182
183
# File 'lib/trestle/adapters/adapter.rb', line 181

def default_form_attributes
  raise NotImplementedError
end

#default_table_attributesObject

Generates a list of attributes that should be rendered by the index (table) view.

Returns an Array of Trestle::Attribute and/or Trestle::Attribute::Association objects.

Raises:

  • (NotImplementedError)

174
175
176
# File 'lib/trestle/adapters/adapter.rb', line 174

def default_table_attributes
  raise NotImplementedError
end

#delete_instance(instance, params = {}) ⇒ Object

Deletes an instance (used by the destroy action).

instance - The instance to delete params - Unfiltered params hash from the controller

Returns a boolean indicating the success/fail status of the deletion.

Raises:

  • (NotImplementedError)

65
66
67
# File 'lib/trestle/adapters/adapter.rb', line 65

def delete_instance(instance, params={})
  raise NotImplementedError
end

#finalize_collection(collection) ⇒ Object

Finalizes a collection so that it can be rendered within the index view.

In most cases (e.g. ActiveRecord), no finalization is required. However if you are using a search library then you may need to explicitly execute the search, or access the models via a #records or #objects method.

collection - The collection to finalize

Returns an enumerable collection of instances.


77
78
79
# File 'lib/trestle/adapters/adapter.rb', line 77

def finalize_collection(collection)
  collection
end

#find_instance(params) ⇒ Object

Finds (and returns) an individual instance for use by the show, edit, update, destroy actions.

params - Unfiltered params hash from the controller

Raises:

  • (NotImplementedError)

26
27
28
# File 'lib/trestle/adapters/adapter.rb', line 26

def find_instance(params)
  raise NotImplementedError
end

#human_attribute_name(attribute, options = {}) ⇒ Object

Produces a human-readable name for a given attribute, applying I18n where appropriate. See ActiveModel::Translation for an implementation of this method.

attribute - Attribute name (Symbol) options - Hash of options [not currently used]

Returns the human-readable name of the given attribute as a String.


167
168
169
# File 'lib/trestle/adapters/adapter.rb', line 167

def human_attribute_name(attribute, options={})
  attribute.to_s.titleize
end

#merge_scopes(scope, other) ⇒ Object

Merges scopes together for Trestle scope application and counting.

scope - The first scope other - The second scope

Returns a scope object representing the combination of the two given scopes.

Raises:

  • (NotImplementedError)

108
109
110
# File 'lib/trestle/adapters/adapter.rb', line 108

def merge_scopes(scope, other)
  raise NotImplementedError
end

#paginate(collection, params) ⇒ Object

Paginates a collection for use by the index action.

collection - The collection to paginate params - Unfiltered params hash from the controller:

:page - current page number

Returns a Kaminari-compatible scope corresponding to a single page.


140
141
142
143
144
145
# File 'lib/trestle/adapters/adapter.rb', line 140

def paginate(collection, params)
  collection = Kaminari.paginate_array(collection.to_a) unless collection.respond_to?(Kaminari.config.page_method_name)
  per_page = admin.pagination_options[:per]

  collection.public_send(Kaminari.config.page_method_name, params[:page]).per(per_page)
end

#permitted_params(params) ⇒ Object

Filters the submitted form parameters and returns a whitelisted attributes 'hash' that can be set or updated on a model instance.

IMPORTANT: By default, all params are permitted, which assumes a trusted administrator. If this is not the case, a `params` block should be individually declared for each admin with the set of permitted parameters.

params - Unfiltered params hash from the controller

Returns the permitted set of parameters as a ActionController::Parameters object.


156
157
158
# File 'lib/trestle/adapters/adapter.rb', line 156

def permitted_params(params)
  params.require(admin.parameter_name).permit!
end

#save_instance(instance, params = {}) ⇒ Object

Saves an instance (used by the create and update actions).

instance - The instance to save params - Unfiltered params hash from the controller

Returns a boolean indicating the success/fail status of the save.

Raises:

  • (NotImplementedError)

55
56
57
# File 'lib/trestle/adapters/adapter.rb', line 55

def save_instance(instance, params={})
  raise NotImplementedError
end

#sort(collection, field, order) ⇒ Object

Sorts the collection by the given field and order. This method is called when an explicit sort column for the given field is not defined.

collection - The collection to sort field - The field to sort by order - Symbol (:asc or :desc) representing the sort order (ascending or descending)

Returns a scope object

Raises:

  • (NotImplementedError)

129
130
131
# File 'lib/trestle/adapters/adapter.rb', line 129

def sort(collection, field, order)
  raise NotImplementedError
end

#to_param(instance) ⇒ Object

Converts an instance to a URL parameter. The result of this method is passed to the #find_instance adapter method as params. It is recommended to simply use the instance's #id, as other potential options such as a permalink/slug could potentially be changed during editing.

instance - The instance to convert

Returns the URL representation of the instance.


98
99
100
# File 'lib/trestle/adapters/adapter.rb', line 98

def to_param(instance)
  instance.id
end

#update_instance(instance, attrs, params = {}) ⇒ Object

Updates (but does not save) a given resource's attributes.

instance - The instance to update attrs - Permitted attributes to update on the instance params - Unfiltered params hash from the controller

The return value is ignored.

Raises:

  • (NotImplementedError)

45
46
47
# File 'lib/trestle/adapters/adapter.rb', line 45

def update_instance(instance, attrs, params={})
  raise NotImplementedError
end