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) }

Instance Method Summary collapse

Instance Method Details

#initialize(*args) ⇒ Object



10
11
12
13
14
15
16
# File 'lib/uchi/field/configuration.rb', line 10

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

#on(*actions) ⇒ self, Array<Symbol>

Sets or gets which actions this field should appear on.

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

Examples:

Setting

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

Getting

field.on # => [:index, :show]

Parameters:

  • actions (Array<Symbol>)

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

Returns:

  • (self, Array<Symbol>)

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



33
34
35
36
37
38
# File 'lib/uchi/field/configuration.rb', line 33

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

  @on = actions.flatten
  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



57
58
59
60
61
62
# File 'lib/uchi/field/configuration.rb', line 57

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, Boolean

Sets or gets whether this field is searchable.

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

Examples:

Setting

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

Parameters:

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

    Whether the field is searchable in index views. Defaults to false for most fields, except text-based fields.

Returns:

  • (self, Boolean)

    Returns self for method chaining when setting, or boolean when getting



77
78
79
80
81
82
# File 'lib/uchi/field/configuration.rb', line 77

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:



86
87
88
89
90
# File 'lib/uchi/field/configuration.rb', line 86

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 { |query, direction|
  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



113
114
115
116
117
118
# File 'lib/uchi/field/configuration.rb', line 113

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

  @sortable = value
  self
end

#sortable?Boolean

Returns true if the field is sortable

Returns:



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

def sortable?
  return default_sortable? if @sortable.nil?

  !!@sortable
end