Class: RecordViewHelper::RecordValueSetting

Inherits:
Object
  • Object
show all
Defined in:
lib/record_view_helper/record_value_setting.rb

Overview

columns with formats, links, etc.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, table_name, only: nil, except: nil, formats: nil, links: nil, attrs: nil, header_attrs: nil) ⇒ RecordValueSetting

Returns a new instance of RecordValueSetting.

Parameters:

  • columns (Array<Symbol>)

    default columns

  • table_name (Symbol)

    default table name

  • only (Array<Symbol>) (defaults to: nil)

    only columns

  • except (Array<Symbol>) (defaults to: nil)

    except columns

  • formats (Hash<Symbol, Symbol|Array<Symbol|Array<Symbol>>>) (defaults to: nil)

    formats

  • links (Hash<Symbol, Symbol|Hash<Symbol, Symbol|Array<Symbol>>>) (defaults to: nil)

    links settings

  • attrs (Hash<Symbol, Hash<Symbol, String>>) (defaults to: nil)

    column value dom tag attrs

  • header_attrs (Hash<Symbol, Hash<Symbol, String>>) (defaults to: nil)

    column header dom tag attrs



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/record_view_helper/record_value_setting.rb', line 44

def initialize( # rubocop:disable Metrics/ParameterLists
  columns,
  table_name,
  only: nil,
  except: nil,
  formats: nil,
  links: nil,
  attrs: nil,
  header_attrs: nil
)
  @columns = columns
  @table_name = table_name
  @only = only || []
  @except = except || []
  @formats = formats || {}
  @links = links || {}
  @attrs = attrs || {}
  @header_attrs = header_attrs || {}
end

Instance Attribute Details

#attrsObject (readonly)

attr settings



12
13
14
# File 'lib/record_view_helper/record_value_setting.rb', line 12

def attrs
  @attrs
end

#formatsObject (readonly)

format settings



8
9
10
# File 'lib/record_view_helper/record_value_setting.rb', line 8

def formats
  @formats
end

#header_attrsObject (readonly)

header attr settings



14
15
16
# File 'lib/record_view_helper/record_value_setting.rb', line 14

def header_attrs
  @header_attrs
end

link settings



10
11
12
# File 'lib/record_view_helper/record_value_setting.rb', line 10

def links
  @links
end

Class Method Details

.build_from_hash!(columns, table_name, options = {}) ⇒ RecordValueSetting

build from hash

caution: this method deletes given hash options’ key

Parameters:

  • columns (Array<Symbol>)

    default columns

  • table_name (Symbol)

    default table name

  • options (Hash) (defaults to: {})

    option hash

Returns:



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/record_view_helper/record_value_setting.rb', line 23

def self.build_from_hash!(columns, table_name, options = {})
  new(
    columns,
    options.delete(:table_name) || table_name,
    only: options.delete(:only),
    except: options.delete(:except),
    formats: options.delete(:formats),
    links: options.delete(:links),
    attrs: options.delete(:attrs),
    header_attrs: options.delete(:header_attrs),
  )
end

Instance Method Details

#attr(column, attr) ⇒ void

This method returns an undefined value.

set column value dom tag attrs

Examples:

= dl_for(record) do |s|
  - s.attr :foo_id, style: "width: 4em"

Parameters:

  • column (Symbol)

    target column

  • attr (Hash<Symbol, String>)

    attr hash



148
149
150
# File 'lib/record_view_helper/record_value_setting.rb', line 148

def attr(column, attr)
  @attrs[column] = attr
end

#column(column, format: nil, link: nil, attr: nil, header_attr: nil) ⇒ void

This method returns an undefined value.

set column

Examples:

= dl_for(record) do |s|
  - s.column :foo_id, format: [:foo_id, [:foo, :name]], link: :foo_path

Parameters:

  • column (Symbol)

    target column

  • format (Proc|Symbol|Array<Symbol|Array<Symbol>>) (defaults to: nil)

    format

  • link (Symbol|Hash<Symbol|Array<Symbol|Array<Symbol>>>) (defaults to: nil)

    setting

  • attr (Hash<Symbol, String>) (defaults to: nil)

    column value dom tag attrs

  • header_attr (Hash<Symbol, String>) (defaults to: nil)

    column header dom tag attrs



175
176
177
178
179
180
# File 'lib/record_view_helper/record_value_setting.rb', line 175

def column(column, format: nil, link: nil, attr: nil, header_attr: nil)
  @formats[column] if format
  @links[column] if link
  @attrs[column] if attr
  @header_attrs[column] = header_attr if header_attr
end

#columnsArray<Symbol>

calculated columns

Returns:

  • (Array<Symbol>)

    (only || default columns) - except



184
185
186
# File 'lib/record_view_helper/record_value_setting.rb', line 184

def columns
  (@only.presence || @columns).map(&:to_sym) - @except.map(&:to_sym)
end

#except(*columns) ⇒ void

This method returns an undefined value.

add except columns

Examples:

= dl_for(record) do |s|
  - s.except :created_at, :updated_at
  - s.except :password

Parameters:

  • columns (Array<Symbol>)


99
100
101
# File 'lib/record_view_helper/record_value_setting.rb', line 99

def except(*columns)
  @except += columns.flatten
end

#format(column, format = nil) {|record| ... } ⇒ void

This method returns an undefined value.

set column format

Examples:

= dl_for(record) do |s|
  - s.only :id, :foo_id, :statuses
  - s.format :foo_id, [:foo_id, [:foo, :name]]
  - s.format :statuses do |record|
    ul
      - record.statuses.split(" ") each do |status|
        li = status

Parameters:

  • column (Symbol)

    target column

  • format (Proc|Symbol|Array<Symbol|Array<Symbol>>) (defaults to: nil)

    format

Yields:

  • (record)

    format function (optional)

Yield Parameters:

  • record (Object)

    record

Yield Returns:

  • (String)

    formatted value



119
120
121
# File 'lib/record_view_helper/record_value_setting.rb', line 119

def format(column, format = nil, &block)
  @formats[column] = block_given? ? block : format
end

#header_attr(column, header_attr) ⇒ void

This method returns an undefined value.

set column header dom tag attrs

Examples:

= dl_for(record) do |s|
  - s.header_attr :foo_id, style: "font-weight: bold"

Parameters:

  • column (Symbol)

    target column

  • header_attr (Hash<Symbol, String>)

    attr hash



160
161
162
# File 'lib/record_view_helper/record_value_setting.rb', line 160

def header_attr(column, header_attr)
  @header_attrs[column] = header_attr
end

This method returns an undefined value.

set column link setting

Examples:

= dl_for(record) do |s|
  - s.link :foo_id, :foo_path
  - s.link :id, foo_bar_path: [:foo_id, :id]
  - s.link(:bar_id) {|record| baz_path(record.bar_id) }

Parameters:

  • column (Symbol)

    target column

  • link (Symbol|Hash<Symbol|Array<Symbol|Array<Symbol>>>) (defaults to: nil)

    setting

Yields:

  • (record)

    format function (optional)

Yield Parameters:

  • record (Object)

    record

Yield Returns:

  • (String)

    link uri string



136
137
138
# File 'lib/record_view_helper/record_value_setting.rb', line 136

def link(column, link = nil, &block)
  @links[column] = block_given? ? block : link
end

#only(*columns) ⇒ void

This method returns an undefined value.

add only columns

Examples:

= dl_for(record) do |s|
  - s.only :id, :name
  - s.only :foo_id

Parameters:

  • columns (Array<Symbol>)


87
88
89
# File 'lib/record_view_helper/record_value_setting.rb', line 87

def only(*columns)
  @only += columns.flatten
end

#table_name(table_name = nil) ⇒ Symbol

set and get table name

Examples:

= dl_for(record) do |s|
  - s.table_name :foos

Parameters:

  • table_name (Symbol) (defaults to: nil)

Returns:

  • (Symbol)


71
72
73
74
75
76
77
# File 'lib/record_view_helper/record_value_setting.rb', line 71

def table_name(table_name = nil)
  if table_name
    @table_name = table_name
  else
    @table_name
  end
end