Class: Selections::FormBuilderExtensions::SelectionTag

Inherits:
Object
  • Object
show all
Defined in:
lib/selections/form_builder_extensions.rb

Overview

:nodoc:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form, object, field, options, html_options) ⇒ SelectionTag

Returns a new instance of SelectionTag.



38
39
40
41
42
43
44
45
46
47
48
# File 'lib/selections/form_builder_extensions.rb', line 38

def initialize(form, object, field, options, html_options)
  @form = form
  @object = object
  @field = field
  @html_options = html_options || {}

  @system_code_name = options[:system_code] || field
  @selection = Selections.model
  @field_id ||= (field.to_s + "_id").to_sym
  @options = options || {}
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def field
  @field
end

#field_idObject (readonly)

Returns the value of attribute field_id.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def field_id
  @field_id
end

#formObject (readonly)

Returns the value of attribute form.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def form
  @form
end

#html_optionsObject (readonly)

Returns the value of attribute html_options.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def html_options
  @html_options
end

#objectObject (readonly)

Returns the value of attribute object.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def object
  @object
end

#optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def options
  @options
end

#selectionObject (readonly)

Returns the value of attribute selection.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def selection
  @selection
end

#system_code_nameObject (readonly)

Returns the value of attribute system_code_name.



36
37
38
# File 'lib/selections/form_builder_extensions.rb', line 36

def system_code_name
  @system_code_name
end

Instance Method Details

#blank_contentObject



111
112
113
# File 'lib/selections/form_builder_extensions.rb', line 111

def blank_content
  options[:blank_content] || 'none'
end

#default_itemObject



107
108
109
# File 'lib/selections/form_builder_extensions.rb', line 107

def default_item
  items.where(is_default: true).first.try(:id).to_s
end

#error_messageObject



95
96
97
# File 'lib/selections/form_builder_extensions.rb', line 95

def error_message
  "Could not find system_code of '#{system_code_name}' or '#{form.object_name}_#{system_code_name}'"
end

#include_blank?Boolean

Returns:

  • (Boolean)


50
51
52
53
54
55
56
# File 'lib/selections/form_builder_extensions.rb', line 50

def include_blank?
  if options[:include_blank].nil?
    !!((object.try(:new_record?) || !object.send(field_id))) && default_item.blank?
  else
    !!options[:include_blank]
  end
end

#itemsObject



63
64
65
# File 'lib/selections/form_builder_extensions.rb', line 63

def items
  @items ||= system_code.children.filter_archived_except_selected(object.send(field_id))
end

#radio_tagObject



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/selections/form_builder_extensions.rb', line 79

def radio_tag
  if system_code
    items.unshift(selection.new(name: blank_content)) if include_blank?

    items.inject('') do |build, item|
      label_html_options = item.id ? html_options.merge(value: item.id.to_s) : html_options
      html_options[:checked] = selected_item == item.id.to_s && !item.new_record?
      build + form.label(field_id, label_html_options) do
        form.radio_button(field_id, item.id, html_options) + item.name
      end
    end.html_safe
  else
    error_message
  end
end

#select_tagObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/selections/form_builder_extensions.rb', line 67

def select_tag
  if system_code
    #TODO add default style
    #html_options[:style] ||=
    options[:include_blank] = include_blank?
    options[:selected] = selected_item
    form.select field_id, items.map { |item| [item.name, item.id] }, options, html_options
  else
    error_message
  end
end

#selected_itemObject



99
100
101
102
103
104
105
# File 'lib/selections/form_builder_extensions.rb', line 99

def selected_item
  if object.new_record? && object.send(field_id).blank?
     default_item
  else
    object.send(field_id).to_s
  end
end

#system_codeObject



58
59
60
61
# File 'lib/selections/form_builder_extensions.rb', line 58

def system_code
  @system_code ||= selection.where(system_code: "#{form.object_name}_#{system_code_name}").first
  @system_code ||= selection.where(system_code: system_code_name.to_s).first
end