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
Instance Attribute Summary collapse
-
#context ⇒ Object
Returns the value of attribute context.
Class Method Summary collapse
-
.all ⇒ Object
Returns all defined Uchi::Repository classes.
-
.any_searchable? ⇒ Boolean
Returns true if at least one repository has a searchable field.
-
.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.
-
#initialize(context: nil) ⇒ Repository
constructor
A new instance of Repository.
- #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.
Constructor Details
#initialize(context: nil) ⇒ Repository
Returns a new instance of Repository.
134 135 136 |
# File 'lib/uchi/repository.rb', line 134 def initialize(context: nil) @context = context || build_default_context end |
Instance Attribute Details
#context ⇒ Object
Returns the value of attribute context.
46 47 48 |
# File 'lib/uchi/repository.rb', line 46 def context @context end |
Class Method Details
.all ⇒ Object
Returns all defined Uchi::Repository classes
11 12 13 14 15 |
# File 'lib/uchi/repository.rb', line 11 def all Uchi::Repositories.constants.sort.map { |const_name| Uchi::Repositories.const_get(const_name) } end |
.any_searchable? ⇒ Boolean
Returns true if at least one repository has a searchable field.
20 21 22 |
# File 'lib/uchi/repository.rb', line 20 def any_searchable? all.any? { |repository_class| repository_class.new.searchable? } 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.
27 28 29 |
# File 'lib/uchi/repository.rb', line 27 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.
32 33 34 |
# File 'lib/uchi/repository.rb', line 32 def for_model(model) all.find { |repository| repository.model.to_s == model.to_s } end |
.model ⇒ Object
Returns the model class this repository manages.
37 38 39 |
# File 'lib/uchi/repository.rb', line 37 def model @model ||= name.demodulize.constantize end |
.model_param_key ⇒ Object
41 42 43 |
# File 'lib/uchi/repository.rb', line 41 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
149 150 151 |
# File 'lib/uchi/repository.rb', line 149 def actions [] end |
#build(attributes = {}) ⇒ Object
Returns a new, unsaved instance of the model this repository manages.
49 50 51 |
# File 'lib/uchi/repository.rb', line 49 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.
56 57 58 |
# File 'lib/uchi/repository.rb', line 56 def controller_name self.class.controller_name end |
#default_sort_order ⇒ Object
60 61 62 |
# File 'lib/uchi/repository.rb', line 60 def default_sort_order SortOrder.new(model.primary_key.to_sym, :asc) end |
#fields_for_edit(record: nil) ⇒ Array<Uchi::Field>
Returns an array of fields to show on the edit page.
69 70 71 72 73 |
# File 'lib/uchi/repository.rb', line 69 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.
78 79 80 |
# File 'lib/uchi/repository.rb', line 78 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.
87 88 89 90 91 |
# File 'lib/uchi/repository.rb', line 87 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.
98 99 100 101 102 |
# File 'lib/uchi/repository.rb', line 98 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
121 122 123 |
# File 'lib/uchi/repository.rb', line 121 def find(id) model.find(id) end |
#find_all(search: nil, scope: model.all, sort_order: default_sort_order) ⇒ Object
104 105 106 107 108 109 |
# File 'lib/uchi/repository.rb', line 104 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.
117 118 119 |
# File 'lib/uchi/repository.rb', line 117 def find_many(ids) model.where(model.primary_key => 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.
130 131 132 |
# File 'lib/uchi/repository.rb', line 130 def includes [] end |
#model ⇒ Object
153 154 155 |
# File 'lib/uchi/repository.rb', line 153 def model self.class.model end |
#model_param_key ⇒ Object
157 158 159 |
# File 'lib/uchi/repository.rb', line 157 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.
165 166 167 |
# File 'lib/uchi/repository.rb', line 165 def routes @routes ||= Routes.new(self) end |
#searchable? ⇒ Boolean
Returns true if this repository has at least one searchable field.
172 173 174 |
# File 'lib/uchi/repository.rb', line 172 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:
titlenameto_s
You can override this method in your repository subclass to provide custom logic.
185 186 187 188 189 190 191 192 193 |
# File 'lib/uchi/repository.rb', line 185 def title(record) return nil unless record [:title, :name, :to_s].each do |method| if record.respond_to?(method) return record.public_send(method) end end end |