Class: Trestle::Adapters::Adapter

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(admin) ⇒ Adapter

Returns a new instance of Adapter.



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

def initialize(admin)
  @admin = admin
end

Instance Attribute Details

#adminObject (readonly)

Returns the value of attribute admin.



4
5
6
# File 'lib/trestle/adapters/adapter.rb', line 4

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)


31
32
33
# File 'lib/trestle/adapters/adapter.rb', line 31

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)


16
17
18
# File 'lib/trestle/adapters/adapter.rb', line 16

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)


112
113
114
# File 'lib/trestle/adapters/adapter.rb', line 112

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.



82
83
84
# File 'lib/trestle/adapters/adapter.rb', line 82

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)


176
177
178
# File 'lib/trestle/adapters/adapter.rb', line 176

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)


169
170
171
# File 'lib/trestle/adapters/adapter.rb', line 169

def default_table_attributes
  raise NotImplementedError
end

#delete_instance(instance) ⇒ Object

Deletes an instance (used by the destroy action).

instance - The instance to delete

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

Raises:

  • (NotImplementedError)


60
61
62
# File 'lib/trestle/adapters/adapter.rb', line 60

def delete_instance(instance)
  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.



72
73
74
# File 'lib/trestle/adapters/adapter.rb', line 72

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)


23
24
25
# File 'lib/trestle/adapters/adapter.rb', line 23

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.



162
163
164
# File 'lib/trestle/adapters/adapter.rb', line 162

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)


103
104
105
# File 'lib/trestle/adapters/adapter.rb', line 103

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.



135
136
137
138
139
140
# File 'lib/trestle/adapters/adapter.rb', line 135

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.



151
152
153
# File 'lib/trestle/adapters/adapter.rb', line 151

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

#save_instance(instance) ⇒ Object

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

instance - The instance to save

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

Raises:

  • (NotImplementedError)


51
52
53
# File 'lib/trestle/adapters/adapter.rb', line 51

def save_instance(instance)
  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)


124
125
126
# File 'lib/trestle/adapters/adapter.rb', line 124

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.



93
94
95
# File 'lib/trestle/adapters/adapter.rb', line 93

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)


42
43
44
# File 'lib/trestle/adapters/adapter.rb', line 42

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