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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.

Returns:

  • (Boolean)


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

Returns:



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.

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:



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.

Returns:



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.

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:



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.

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:



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.

Parameters:

  • ids (Array<Integer>) —

    The IDs of the records to find

Returns:

  • (ActiveRecord::Relation) —

    The found records



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.

Returns:

  • (Boolean)


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:

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

#translate ⇒ Object

Provides access to translation helpers specific to this repository.



196
197
198
# File 'lib/uchi/repository.rb', line 196

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