Module: Uchi::Field::Configuration

Included in:
Uchi::Field
Defined in:
lib/uchi/field/configuration.rb

Defined Under Namespace

Classes: Unset

Constant Summary collapse

DEFAULT_READER =
->(record, field_name) { record&.public_send(field_name) }
DEFAULT_VISIBLE =
->(_record) { true }

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/uchi/field/configuration.rb', line 13

def initialize(*args)
  super
  @on = default_on
  @reader = DEFAULT_READER
  @searchable = default_searchable?
  @visible = DEFAULT_VISIBLE
  @sortable = default_sortable
end

#on(*views) ⇒ self, Array<Uchi::View>

Sets or gets which views this field should appear on.

When called with arguments, sets the views and returns self for chaining. When called without arguments, returns the current views.

Examples:

Setting

Field::Number.new(:id).on(:index, :show)

Getting

field.on # => [Uchi::View.new(:index), Uchi::View.new(:show)]

Parameters:

  • views (Array<Symbol, String, Uchi::View>) —

    The views where this field should appear (e.g., :index, :show, :new, :edit)

Returns:

  • (self, Array<Uchi::View>) —

    Returns self for method chaining when setting, or the views array when getting



37
38
39
40
41
42
# File 'lib/uchi/field/configuration.rb', line 37

def on(*views)
  return @on if views.empty?

  @on = views.flatten.map { |view| Uchi::View.new(view) }
  self
end

#reader(reader_proc = nil) ⇒ self, Proc

Sets or gets a custom reader for this field.

When called with an argument, sets the reader and returns self for chaining. When called without arguments, returns the current reader.

Examples:

Setting

Field::String.new(:full_name).reader(->(record, field_name) {
  "#{record.first_name} #{record.last_name}"
})

Getting

field.reader # => #<Proc...>

Parameters:

  • reader_proc (Proc, nil) (defaults to: nil) —

    A callable that reads the value from a record. The proc receives the model and field name, and should return the value.

Returns:

  • (self, Proc) —

    Returns self for method chaining when setting, or the reader proc when getting



61
62
63
64
65
66
# File 'lib/uchi/field/configuration.rb', line 61

def reader(reader_proc = nil)
  return @reader if reader_proc.nil? && !block_given?

  @reader = reader_proc || Proc.new
  self
end

#searchable(value = Configuration::Unset) ⇒ self, ...

Sets or gets whether and how this field is searchable.

When called with an argument, sets searchable and returns self for chaining. When called without arguments, returns the searchable value.

Examples:

Setting with boolean

Field::String.new(:password).searchable(false)
Field::Number.new(:id).searchable(true)

Setting with lambda

Field::BelongsTo.new(:company).searchable(lambda { |context:, query:, term:|
  query.joins(:company).where("companies.name LIKE ?", "%#{term}%")
})

Parameters:

  • value (Boolean, Proc, nil) (defaults to: Configuration::Unset) —

    Whether the field is searchable in index views. Defaults to false for most fields, except text-based fields. Pass a lambda that receives the query and search term and returns an ActiveRecord::Relation to search across an association.

Returns:

  • (self, Boolean, Proc) —

    Returns self for method chaining when setting, or the searchable value when getting



88
89
90
91
92
93
# File 'lib/uchi/field/configuration.rb', line 88

def searchable(value = Configuration::Unset)
  return @searchable if value == Configuration::Unset

  @searchable = value
  self
end

#searchable? ⇒ Boolean

Returns true if the field is searchable and should be included in the query when a search term has been entered.

Returns:



97
98
99
100
101
# File 'lib/uchi/field/configuration.rb', line 97

def searchable?
  return default_searchable? if @searchable.nil?

  !!@searchable
end

#sortable(value = Configuration::Unset) ⇒ self, ...

Sets or gets whether and how this field is sortable.

When called with an argument, sets sortable and returns self for chaining. When called without arguments, returns the sortable value.

Examples:

Setting with boolean

Field::Number.new(:calculated_sum).sortable(false)

Setting with lambda

Field::Number.new(:users_count).sortable(lambda { |context:, direction:, query:|
  query.joins(:users).group(:id).order("COUNT(users.id) #{direction}")
})

Getting

field.sortable # => true

Parameters:

  • value (Boolean, Proc, Symbol) (defaults to: Configuration::Unset) —

    Whether the field is sortable. Pass true/false for simple sorting, or a lambda that receives the query and direction and returns an ActiveRecord::Relation for custom sorting.

Returns:

  • (self, Boolean, Proc) —

    Returns self for method chaining when setting, or the sortable value when getting



150
151
152
153
154
155
# File 'lib/uchi/field/configuration.rb', line 150

def sortable(value = Configuration::Unset)
  return @sortable.nil? ? default_sortable : @sortable if value == Configuration::Unset

  @sortable = value
  self
end

#sortable? ⇒ Boolean

Returns true if the field is sortable

Returns:



158
159
160
# File 'lib/uchi/field/configuration.rb', line 158

def sortable?
  !!sortable
end

#visible(visible_proc = Configuration::Unset) ⇒ self, Proc

Sets or gets a conditional proc that determines whether this field should be visible for a given record.

When called with a proc argument, sets the visibility condition and returns self for chaining. When called without arguments, returns the current proc.

Examples:

Setting

Field::String.new(:id).visible(lambda { |record| record.id.even? })

Getting

field.visible # => #<Proc...>

Parameters:

  • visible_proc (Proc) (defaults to: Configuration::Unset) —

    A callable that receives the record and returns a boolean indicating whether the field should be visible. Raises ArgumentError for non-callables.

Returns:

  • (self, Proc) —

    Returns self for method chaining when setting, or the current proc when getting

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
# File 'lib/uchi/field/configuration.rb', line 120

def visible(visible_proc = Configuration::Unset)
  return @visible if visible_proc == Configuration::Unset

  raise ArgumentError, "visible must be callable" unless visible_proc.respond_to?(:call)

  @visible = visible_proc
  self
end

#visible_for?(record) ⇒ Boolean

Returns whether this field should be visible for the given record.

Calls the visible proc with the record. Defaults to DEFAULT_VISIBLE, which always returns true.

Parameters:

  • record (Object) —

    The record to check visibility for

Returns:

  • (Boolean) —

    Whether the field should be visible



169
170
171
# File 'lib/uchi/field/configuration.rb', line 169

def visible_for?(record)
  !!@visible.call(record)
end