Class: Uchi::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/uchi/repository.rb,
lib/uchi/repository/routes.rb,
lib/uchi/repository/translate.rb

Defined Under Namespace

Classes: Routes, Translate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allObject

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_nameObject

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

.modelObject

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_keyObject



32
33
34
# File 'lib/uchi/repository.rb', line 32

def model_param_key
  model.model_name.param_key
end

Instance Method Details

#actionsArray<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

Returns:



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_nameObject

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_orderObject



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.

Parameters:

  • record (Object, nil) (defaults to: nil)

    The record being edited. When provided, fields with a visibility condition are filtered by it. When omitted, all edit fields are returned.

Returns:



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_indexArray<Uchi::Field>

Returns an array of fields to show on the index page.

Returns:



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.

Parameters:

  • record (Object, nil) (defaults to: nil)

    The record being created. When provided, fields with a visibility condition are filtered by it. When omitted, all new fields are returned.

Returns:



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.

Parameters:

  • record (Object, nil) (defaults to: nil)

    The record being shown. When provided, fields with a visibility condition are filtered by it. When omitted, all show fields are returned.

Returns:



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.

Parameters:

  • ids (Array<Integer>)

    The IDs of the records to find

Returns:

  • (ActiveRecord::Relation)

    The found records



106
107
108
# File 'lib/uchi/repository.rb', line 106

def find_many(ids)
  model.where(id: ids)
end

#includesObject

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

#modelObject



138
139
140
# File 'lib/uchi/repository.rb', line 138

def model
  self.class.model
end

#model_param_keyObject



142
143
144
# File 'lib/uchi/repository.rb', line 142

def model_param_key
  self.class.model_param_key
end

#routesUchi::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.

Returns:

  • (Boolean)


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:

  1. name
  2. title
  3. to_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

#translateObject

Provides access to translation helpers specific to this repository.



181
182
183
# File 'lib/uchi/repository.rb', line 181

def translate
  @translate ||= Translate.new(repository: self)
end