Class: Terrazzo::Field::HasMany

Inherits:
Associative show all
Defined in:
lib/terrazzo/field/has_many.rb

Constant Summary

Constants inherited from Base

Base::ABSTRACT_FIELD_CLASSES

Instance Attribute Summary

Attributes inherited from Base

#attribute, #data, #options, #page, #resource

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Associative

associative?, eager_load?

Methods inherited from Base

associative?, eager_load?, #field_type, field_type, #form_input_attributes, #initialize, #required?, searchable?, transform_param, with_options

Constructor Details

This class inherits a constructor from Terrazzo::Field::Base

Class Method Details

.default_optionsObject



38
39
40
# File 'lib/terrazzo/field/has_many.rb', line 38

def default_options
  {}
end

.permitted_attribute(attr, _options = {}) ⇒ Object



42
43
44
# File 'lib/terrazzo/field/has_many.rb', line 42

def permitted_attribute(attr, _options = {})
  { "#{attr.to_s.singularize}_ids" => [] }
end

.sortable?Boolean

Returns:



34
35
36
# File 'lib/terrazzo/field/has_many.rb', line 34

def sortable?
  false
end

Instance Method Details

#current_pageObject



51
52
53
54
# File 'lib/terrazzo/field/has_many.rb', line 51

def current_page
  p = options[:_page].to_i
  p < 1 ? 1 : p
end

#page_recordsObject



64
65
66
# File 'lib/terrazzo/field/has_many.rb', line 64

def page_records
  paginated.to_a
end

#per_pageObject



47
48
49
# File 'lib/terrazzo/field/has_many.rb', line 47

def per_page
  (options[:per_page] || options[:limit] || 5).to_i
end

#serializable_options(page = nil) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/terrazzo/field/has_many.rb', line 22

def serializable_options(page = nil)
  opts = {}
  if page == :form && resource
    opts[:resourceOptions] = resource_options
  end
  if options.key?(:render_actions)
    opts[:renderActions] = options[:render_actions]
  end
  opts
end

#serialize_value(mode) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/terrazzo/field/has_many.rb', line 4

def serialize_value(mode)
  return nil if data.nil?

  case mode
  when :index
    count = data.size
    label = attribute.to_s.humanize.downcase
    label = label.singularize if count == 1
    { count: count, label: label }
  when :form
    data.map { |r| r.id.to_s }
  when :show
    serialize_show_value
  else
    data.map { |r| { id: r.id.to_s, display: display_name(r) } }
  end
end

#total_countObject



56
57
58
# File 'lib/terrazzo/field/has_many.rb', line 56

def total_count
  paginated.total_count
end

#total_pagesObject



60
61
62
# File 'lib/terrazzo/field/has_many.rb', line 60

def total_pages
  [paginated.total_pages, 1].max
end