Class: Carnival::NestedFormOptions
- Inherits:
-
Object
- Object
- Carnival::NestedFormOptions
- Defined in:
- app/view_objects/carnival/nested_form_options.rb
Instance Method Summary collapse
- #available_options ⇒ Object
- #check_box_html ⇒ Object
- #controller_to_field ⇒ Object
- #field ⇒ Object
- #field_class_file_name ⇒ Object
- #field_name ⇒ Object
- #field_name_identifier ⇒ Object
- #has_new_action? ⇒ Boolean
-
#initialize(model, presenter, field, items = nil) ⇒ NestedFormOptions
constructor
A new instance of NestedFormOptions.
- #make_html_id_for(elem) ⇒ Object
- #model ⇒ Object
- #model_class_name_underscore ⇒ Object
- #model_has_this_item?(id) ⇒ Boolean
- #option_css_class(item) ⇒ Object
- #option_id(item_option) ⇒ Object
- #option_name ⇒ Object
- #presenter ⇒ Object
- #scope_column_name ⇒ Object
- #scope_html_id ⇒ Object
- #scopeJSFunction ⇒ Object
- #select_options ⇒ Object
- #select_tag_id ⇒ Object
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 if items.nil? end |
Instance Method Details
#available_options ⇒ Object
33 34 35 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 33 def @available_options end |
#check_box_html ⇒ Object
81 82 83 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 81 def check_box_html end |
#controller_to_field ⇒ Object
53 54 55 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 53 def controller_to_field @presenter.controller_to_field @field end |
#field ⇒ Object
17 18 19 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 17 def field @field end |
#field_class_file_name ⇒ Object
49 50 51 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 49 def field_class_file_name @presenter. @field.name.to_sym end |
#field_name ⇒ Object
29 30 31 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 29 def field_name @field.name end |
#field_name_identifier ⇒ Object
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
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 |
#model ⇒ Object
25 26 27 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 25 def model @model end |
#model_class_name_underscore ⇒ Object
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
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_name ⇒ Object
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 |
#presenter ⇒ Object
21 22 23 |
# File 'app/view_objects/carnival/nested_form_options.rb', line 21 def presenter @presenter end |
#scope_column_name ⇒ Object
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_id ⇒ Object
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 |
#scopeJSFunction ⇒ Object
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_options ⇒ Object
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 = [] << [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) << [item.to_label, id, props] end end |
#select_tag_id ⇒ Object
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 |