Class: Uchi::Repository
- Inherits:
-
Object
- Object
- Uchi::Repository
- Defined in:
- lib/uchi/repository.rb,
lib/uchi/repository/routes.rb,
lib/uchi/repository/translate.rb
Defined Under Namespace
Class Method Summary collapse
-
.all ⇒ Object
Returns all defined Uchi::Repository classes.
-
.controller_name ⇒ Object
Returns the “name” of the controller that handles requests for this repository.
-
.for_model(model) ⇒ Object
Returns the repository for the given model, or nil if none is found.
-
.model ⇒ Object
Returns the model class this repository manages.
- .model_param_key ⇒ Object
Instance Method Summary collapse
-
#actions ⇒ Array<Uchi::Action>
Returns the list of actions available for this repository.
-
#build(attributes = {}) ⇒ Object
Returns a new, unsaved instance of the model this repository manages.
-
#controller_name ⇒ Object
Returns the “name” of the controller that handles requests for this repository.
- #default_sort_order ⇒ Object
-
#fields_for_edit ⇒ Object
Returns an array of fields to show on the edit page.
-
#fields_for_index ⇒ Object
Returns an array of fields to show on the index page.
-
#fields_for_new ⇒ Object
Returns an array of fields to show on the new page.
-
#fields_for_show ⇒ Object
Returns an array of fields to show on the show page.
- #find(id) ⇒ Object
- #find_all(search: nil, scope: model.all, sort_order: default_sort_order) ⇒ Object
-
#find_many(ids) ⇒ ActiveRecord::Relation
Finds multiple records by their IDs.
-
#includes ⇒ Object
Returns the list of associations to include when querying for records.
- #model ⇒ Object
- #model_param_key ⇒ Object
-
#routes ⇒ Uchi::Repository::Routes
Returns an instance of Uchi::Repository::Routes for this repository, which can be used to generate paths and URLs.
-
#searchable? ⇒ Boolean
Returns true if this repository has at least one searchable field.
-
#title(record) ⇒ Object
Returns the title to show for a given record.
-
#translate ⇒ Object
Provides access to translation helpers specific to this repository.
Class Method Details
.all ⇒ Object
Returns all defined Uchi::Repository classes
9 10 11 12 13 |
# File 'lib/uchi/repository.rb', line 9 def all Uchi::Repositories.constants.map { |const_name| Uchi::Repositories.const_get(const_name) } end |
.controller_name ⇒ Object
Returns the “name” of the controller that handles requests for this repository. Note that this is different from the controllers class name and is intended for generating URLs.
18 19 20 |
# File 'lib/uchi/repository.rb', line 18 def controller_name model_param_key.pluralize end |
.for_model(model) ⇒ Object
Returns the repository for the given model, or nil if none is found.
23 24 25 |
# File 'lib/uchi/repository.rb', line 23 def for_model(model) all.find { |repository| repository.model.to_s == model.to_s } end |
.model ⇒ Object
Returns the model class this repository manages.
28 29 30 |
# File 'lib/uchi/repository.rb', line 28 def model @model ||= name.demodulize.constantize end |
.model_param_key ⇒ Object
32 33 34 |
# File 'lib/uchi/repository.rb', line 32 def model_param_key model.model_name.param_key end |
Instance Method Details
#actions ⇒ Array<Uchi::Action>
Returns the list of actions available for this repository.
Actions are instances of Uchi::Action subclasses that can be executed on one or more records.
Example:
def actions
[PublishPost.new, ExportToCsv.new]
end
114 115 116 |
# File 'lib/uchi/repository.rb', line 114 def actions [] end |
#build(attributes = {}) ⇒ Object
Returns a new, unsaved instance of the model this repository manages.
38 39 40 |
# File 'lib/uchi/repository.rb', line 38 def build(attributes = {}) model.new(attributes) end |
#controller_name ⇒ Object
Returns the “name” of the controller that handles requests for this repository. Note that this is different from the controllers class name and is intended for generating URLs.
45 46 47 |
# File 'lib/uchi/repository.rb', line 45 def controller_name self.class.controller_name end |
#default_sort_order ⇒ Object
49 50 51 |
# File 'lib/uchi/repository.rb', line 49 def default_sort_order SortOrder.new(:id, :asc) end |
#fields_for_edit ⇒ Object
Returns an array of fields to show on the edit page.
54 55 56 |
# File 'lib/uchi/repository.rb', line 54 def fields_for_edit fields.select { |field| field.on.include?(:edit) }.each { |field| field.repository = self } end |
#fields_for_index ⇒ Object
Returns an array of fields to show on the index page.
59 60 61 |
# File 'lib/uchi/repository.rb', line 59 def fields_for_index fields.select { |field| field.on.include?(:index) }.each { |field| field.repository = self } end |
#fields_for_new ⇒ Object
Returns an array of fields to show on the new page.
64 65 66 |
# File 'lib/uchi/repository.rb', line 64 def fields_for_new fields.select { |field| field.on.include?(:new) }.each { |field| field.repository = self } end |
#fields_for_show ⇒ Object
Returns an array of fields to show on the show page.
69 70 71 |
# File 'lib/uchi/repository.rb', line 69 def fields_for_show fields.select { |field| field.on.include?(:show) }.each { |field| field.repository = self } end |
#find(id) ⇒ Object
90 91 92 |
# File 'lib/uchi/repository.rb', line 90 def find(id) model.find(id) end |
#find_all(search: nil, scope: model.all, sort_order: default_sort_order) ⇒ Object
73 74 75 76 77 78 |
# File 'lib/uchi/repository.rb', line 73 def find_all(search: nil, scope: model.all, sort_order: default_sort_order) scope ||= model.all query = scope.includes(includes) query = apply_search(query, search) apply_sort_order(query, sort_order) end |
#find_many(ids) ⇒ ActiveRecord::Relation
Finds multiple records by their IDs. If a record is not found, it is ignored.
86 87 88 |
# File 'lib/uchi/repository.rb', line 86 def find_many(ids) model.where(id: ids) end |
#includes ⇒ Object
Returns the list of associations to include when querying for records.
See api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-includes for further details.
99 100 101 |
# File 'lib/uchi/repository.rb', line 99 def includes [] end |
#model ⇒ Object
118 119 120 |
# File 'lib/uchi/repository.rb', line 118 def model self.class.model end |
#model_param_key ⇒ Object
122 123 124 |
# File 'lib/uchi/repository.rb', line 122 def model_param_key self.class.model_param_key end |
#routes ⇒ Uchi::Repository::Routes
Returns an instance of Uchi::Repository::Routes for this repository, which can be used to generate paths and URLs.
130 131 132 |
# File 'lib/uchi/repository.rb', line 130 def routes @routes ||= Routes.new(self) end |
#searchable? ⇒ Boolean
Returns true if this repository has at least one searchable field.
137 138 139 |
# File 'lib/uchi/repository.rb', line 137 def searchable? searchable_fields.any? end |
#title(record) ⇒ Object
Returns the title to show for a given record. By default, this method returns the value of the first of the following methods that exist:
-
name -
title -
to_s
You can override this method in your repository subclass to provide custom logic.
150 151 152 153 154 155 156 157 158 |
# File 'lib/uchi/repository.rb', line 150 def title(record) return nil unless record [:name, :title, :to_s].each do |method| if record.respond_to?(method) return record.public_send(method) end end end |
#translate ⇒ Object
Provides access to translation helpers specific to this repository.
161 162 163 |
# File 'lib/uchi/repository.rb', line 161 def translate @translate ||= Translate.new(repository: self) end |