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
- #initialize(*args) ⇒ Object
-
#on(*actions) ⇒ self, Array<Symbol>
Sets or gets which actions this field should appear on.
-
#reader(reader_proc = nil) ⇒ self, Proc
Sets or gets a custom reader for this field.
-
#searchable(value = Configuration::Unset) ⇒ self, Boolean
Sets or gets whether this field is searchable.
-
#searchable? ⇒ Boolean
Returns true if the field is searchable and should be included in the query when a search term has been entered.
-
#sortable(value = Configuration::Unset) ⇒ self, ...
Sets or gets whether and how this field is sortable.
-
#sortable? ⇒ Boolean
Returns true if the field is sortable.
-
#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.
-
#visible_for?(record) ⇒ Boolean
Returns whether this field should be visible for the given record.
Instance Method Details
#initialize(*args) ⇒ Object
11 12 13 14 15 16 17 18 |
# File 'lib/uchi/field/configuration.rb', line 11 def initialize(*args) super @on = default_on @reader = DEFAULT_READER @searchable = default_searchable? @visible = DEFAULT_VISIBLE @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.
35 36 37 38 39 40 |
# File 'lib/uchi/field/configuration.rb', line 35 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.
59 60 61 62 63 64 |
# File 'lib/uchi/field/configuration.rb', line 59 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.
79 80 81 82 83 84 |
# File 'lib/uchi/field/configuration.rb', line 79 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.
88 89 90 91 92 |
# File 'lib/uchi/field/configuration.rb', line 88 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.
141 142 143 144 145 146 |
# File 'lib/uchi/field/configuration.rb', line 141 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
149 150 151 |
# File 'lib/uchi/field/configuration.rb', line 149 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.
111 112 113 114 115 116 117 118 |
# File 'lib/uchi/field/configuration.rb', line 111 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.
160 161 162 |
# File 'lib/uchi/field/configuration.rb', line 160 def visible_for?(record) !!@visible.call(record) end |