Class: Carnival::NestedFormOptions

Inherits:
Object
  • Object
show all
Defined in:
app/view_objects/carnival/nested_form_options.rb

Instance Method Summary collapse

Constructor Details

#initialize(model, presenter, field, items = nil) ⇒ NestedFormOptions

Returns a new instance of NestedFormOptions.



7
8
9
10
11
12
13
14
15
# File 'app/view_objects/carnival/nested_form_options.rb', line 7

def initialize(model, presenter, field, items = nil)
  @model = model
  @presenter = presenter
  @field = field
  @available_options = items
  @model_fields_ids = []
  populate_model_items
  populate_available_options if items.nil?
end

Instance Method Details

#available_optionsObject



33
34
35
# File 'app/view_objects/carnival/nested_form_options.rb', line 33

def available_options
  @available_options
end

#check_box_htmlObject



81
82
83
# File 'app/view_objects/carnival/nested_form_options.rb', line 81

def check_box_html

end

#controller_to_fieldObject



53
54
55
# File 'app/view_objects/carnival/nested_form_options.rb', line 53

def controller_to_field
  @presenter.controller_to_field @field 
end

#fieldObject



17
18
19
# File 'app/view_objects/carnival/nested_form_options.rb', line 17

def field
  @field
end

#field_class_file_nameObject



49
50
51
# File 'app/view_objects/carnival/nested_form_options.rb', line 49

def field_class_file_name
  @presenter.get_related_class @field.name.to_sym
end

#field_nameObject



29
30
31
# File 'app/view_objects/carnival/nested_form_options.rb', line 29

def field_name
  @field.name
end

#field_name_identifierObject



57
58
59
# File 'app/view_objects/carnival/nested_form_options.rb', line 57

def field_name_identifier
  self.field_name.to_s.singularize.to_sym
end

#has_new_action?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'app/view_objects/carnival/nested_form_options.rb', line 37

def has_new_action?
  @field.nested_form_modes?(:new)
end

#make_html_id_for(elem) ⇒ Object



126
127
128
129
130
131
132
# File 'app/view_objects/carnival/nested_form_options.rb', line 126

def make_html_id_for(elem)
  first = @model.class.name.gsub(/::/, '_').
  gsub(/([a-z\d])([A-Z])/,'\1_\2').
  tr("-", "_").
  downcase
  "#{first}_#{elem}"
end

#modelObject



25
26
27
# File 'app/view_objects/carnival/nested_form_options.rb', line 25

def model
  @model 
end

#model_class_name_underscoreObject



45
46
47
# File 'app/view_objects/carnival/nested_form_options.rb', line 45

def model_class_name_underscore
  @model.class.name.underscore.gsub('/', '_')
end

#model_has_this_item?(id) ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'app/view_objects/carnival/nested_form_options.rb', line 41

def model_has_this_item? id
  @model_fields_ids.include? id
end

#option_css_class(item) ⇒ Object



73
74
75
# File 'app/view_objects/carnival/nested_form_options.rb', line 73

def option_css_class item
  return "carnival-form-options-hide" if !self.model_has_this_item?(item.id)
end

#option_id(item_option) ⇒ Object



67
68
69
70
71
# File 'app/view_objects/carnival/nested_form_options.rb', line 67

def option_id item_option
  model_id = self.model_class_name_underscore
  field_id = self.field_name_identifier
  "#{model_id}_#{field_id}_ids_#{item_option.id}"
end

#option_nameObject



61
62
63
64
65
# File 'app/view_objects/carnival/nested_form_options.rb', line 61

def option_name
  model_id = self.model_class_name_underscore
  field_id = self.field_name_identifier
  "#{model_id}[#{field_id}_ids][]"
end

#presenterObject



21
22
23
# File 'app/view_objects/carnival/nested_form_options.rb', line 21

def presenter
  @presenter 
end

#scope_column_nameObject



103
104
105
106
107
# File 'app/view_objects/carnival/nested_form_options.rb', line 103

def scope_column_name
  scope = @field.nested_form_scope
  return nil if scope.blank? or !@model.class.reflections[scope]
  fkey = @model.class.reflections[scope].foreign_key
end

#scope_html_idObject



99
100
101
# File 'app/view_objects/carnival/nested_form_options.rb', line 99

def scope_html_id
  make_html_id_for scope_column_name
end

#scopeJSFunctionObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/view_objects/carnival/nested_form_options.rb', line 109

def scopeJSFunction
  url = "#{@presenter.model_path(:index)}.json"
  function_params = {}
  function_params[:url] = url
  function_params[:list_scope] = true
  function_params[:model_class_name] = @model.class.name
  function_params[:model_id] = @model.id
  function_params[:field_name] = @field.name
  function_params[:scope_name] = scope_column_name
  function_params[:scope_html_id] = scope_html_id
  function_params[:presenter_name] = @presenter.class.name
  function_params[:controller_name] = @presenter.controller_class_name
  function_params[:has_new_action] = self.has_new_action?

  "getNestedFormOptions(#{function_params.to_json});"
end

#select_optionsObject



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/view_objects/carnival/nested_form_options.rb', line 85

def select_options
  options = []
  options << [I18n.t('nested_form.choose_an_option'), -1]
  @available_options.each do |item|
    props = {}
    id = self.option_id(item)
    props[:id] = "#{id}_select_option"
    props[:data] = {:id => item.id}
    props[:disabled] = self.model_has_this_item?(item.id)
    options << [item.to_label, id, props]
  end
  options
end

#select_tag_idObject



77
78
79
# File 'app/view_objects/carnival/nested_form_options.rb', line 77

def select_tag_id
  "carnival-options-select-#{self.field_name_identifier}"
end