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(record: nil) ⇒ Array<Uchi::Field>
Returns an array of fields to show on the edit page.
-
#fields_for_index ⇒ Array<Uchi::Field>
Returns an array of fields to show on the index page.
-
#fields_for_new(record: nil) ⇒ Array<Uchi::Field>
Returns an array of fields to show on the new page.
-
#fields_for_show(record: nil) ⇒ Array<Uchi::Field>
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
134 135 136 |
# File 'lib/uchi/repository.rb', line 134 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(record: nil) ⇒ Array<Uchi::Field>
Returns an array of fields to show on the edit page.
58 59 60 61 62 |
# File 'lib/uchi/repository.rb', line 58 def fields_for_edit(record: nil) return fields_for(:edit) if record.nil? fields_for(:edit).select { |field| field.visible_for?(record) } end |
#fields_for_index ⇒ Array<Uchi::Field>
Returns an array of fields to show on the index page.
67 68 69 |
# File 'lib/uchi/repository.rb', line 67 def fields_for_index fields_for(:index) end |
#fields_for_new(record: nil) ⇒ Array<Uchi::Field>
Returns an array of fields to show on the new page.
76 77 78 79 80 |
# File 'lib/uchi/repository.rb', line 76 def fields_for_new(record: nil) return fields_for(:new) if record.nil? fields_for(:new).select { |field| field.visible_for?(record) } end |
#fields_for_show(record: nil) ⇒ Array<Uchi::Field>
Returns an array of fields to show on the show page.
87 88 89 90 91 |
# File 'lib/uchi/repository.rb', line 87 def fields_for_show(record: nil) return fields_for(:show) if record.nil? fields_for(:show).select { |field| field.visible_for?(record) } end |
#find(id) ⇒ Object
110 111 112 |
# File 'lib/uchi/repository.rb', line 110 def find(id) model.find(id) end |
#find_all(search: nil, scope: model.all, sort_order: default_sort_order) ⇒ Object
93 94 95 96 97 98 |
# File 'lib/uchi/repository.rb', line 93 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.
106 107 108 |
# File 'lib/uchi/repository.rb', line 106 def find_many(ids) model.where(id: ids) end |
#includes ⇒ Object
Returns the list of associations to include when querying for records.
See https://api.rubyonrails.org/classes/ActiveRecord/QueryMethods.html#method-i-includes for further details.
119 120 121 |
# File 'lib/uchi/repository.rb', line 119 def includes [] end |
#model ⇒ Object
138 139 140 |
# File 'lib/uchi/repository.rb', line 138 def model self.class.model end |
#model_param_key ⇒ Object
142 143 144 |
# File 'lib/uchi/repository.rb', line 142 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.
150 151 152 |
# File 'lib/uchi/repository.rb', line 150 def routes @routes ||= Routes.new(self) end |
#searchable? ⇒ Boolean
Returns true if this repository has at least one searchable field.
157 158 159 |
# File 'lib/uchi/repository.rb', line 157 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:
nametitleto_s
You can override this method in your repository subclass to provide custom logic.
170 171 172 173 174 175 176 177 178 |
# File 'lib/uchi/repository.rb', line 170 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 |