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.
-
.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.
Instance Method Summary collapse
-
#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_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
-
#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 |
.for_model(model) ⇒ Object
Returns the repository for the given model, or nil if none is found.
16 17 18 |
# File 'lib/uchi/repository.rb', line 16 def for_model(model) all.find { |repository| repository.model.to_s == model.to_s } end |
.model ⇒ Object
Returns the model class this repository manages.
21 22 23 |
# File 'lib/uchi/repository.rb', line 21 def model @model ||= name.demodulize.constantize end |
Instance Method Details
#build(attributes = {}) ⇒ Object
Returns a new, unsaved instance of the model this repository manages.
27 28 29 |
# File 'lib/uchi/repository.rb', line 27 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.
34 35 36 |
# File 'lib/uchi/repository.rb', line 34 def controller_name model_param_key.pluralize end |
#default_sort_order ⇒ Object
38 39 40 |
# File 'lib/uchi/repository.rb', line 38 def default_sort_order SortOrder.new(:id, :asc) end |
#fields_for_edit ⇒ Object
Returns an array of fields to show on the edit page.
43 44 45 |
# File 'lib/uchi/repository.rb', line 43 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.
48 49 50 |
# File 'lib/uchi/repository.rb', line 48 def fields_for_index fields.select { |field| field.on.include?(:index) }.each { |field| field.repository = self } end |
#fields_for_show ⇒ Object
Returns an array of fields to show on the show page.
53 54 55 |
# File 'lib/uchi/repository.rb', line 53 def fields_for_show fields.select { |field| field.on.include?(:show) }.each { |field| field.repository = self } end |
#find(id) ⇒ Object
64 65 66 |
# File 'lib/uchi/repository.rb', line 64 def find(id) model.find(id) end |
#find_all(search: nil, scope: model.all, sort_order: default_sort_order) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/uchi/repository.rb', line 57 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 |
#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.
73 74 75 |
# File 'lib/uchi/repository.rb', line 73 def includes [] end |
#model ⇒ Object
77 78 79 |
# File 'lib/uchi/repository.rb', line 77 def model self.class.model end |
#model_param_key ⇒ Object
81 82 83 |
# File 'lib/uchi/repository.rb', line 81 def model_param_key model.model_name.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.
89 90 91 |
# File 'lib/uchi/repository.rb', line 89 def routes @routes ||= Routes.new(self) end |
#searchable? ⇒ Boolean
Returns true if this repository has at least one searchable field.
96 97 98 |
# File 'lib/uchi/repository.rb', line 96 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.
109 110 111 112 113 114 115 116 117 |
# File 'lib/uchi/repository.rb', line 109 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.
120 121 122 |
# File 'lib/uchi/repository.rb', line 120 def translate @translate ||= Translate.new(repository: self) end |