class Object
Public Instance Methods
active_class(attr)
click to toggle source
The attribute is active if it is defined on the collection (this can be the case when setting it nil and a validation of another attribute failed) or the collection has no mixed values of that attribute
# File lib/record_collection/rails/form_builder.rb, line 71 def active_class(attr) active = false # default if object.respond_to?(:mixed_values_for_attribute?) active = !object.mixed_values_for_attribute?(attr, set_if_nil: true) end active = true unless object[attr].nil? # Activate if collection or record attribute is not nil active ? 'active' : 'inactive' end
add_collection_ids(content)
click to toggle source
This method adds the collection ids to the form if not already added. For the lazy peaple that forget to add them manually and use an optional tag
# File lib/record_collection/rails/form_builder.rb, line 5 def add_collection_ids(content) return content if @collection_ids_already_added collection_ids + content end
collection_ids()
click to toggle source
Return inputs for the collection ids
# File lib/record_collection/rails/form_builder.rb, line 11 def collection_ids @collection_ids_already_added = true return "".html_safe unless object.respond_to?(:map) @template.hidden_field_tag('ids', object.map(&:id).join(RecordCollection.ids_separator), id: nil).html_safe end
collection_update()
click to toggle source
# File lib/generators/collection_scaffold/templates/controller.rb, line 59 def collection_update @collection = <%= class_name %>::Collection.find(params[:ids]) if @collection.update params[:collection] redirect_to <%= index_helper %>_path, notice: t('action.collection_update.successful', model: <%= class_name %>.model_name.human) else render :collection_edit end end private # Use callbacks to share common setup or constraints between actions. def set_<%= singular_table_name %> @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> end # Only allow a trusted parameter "white list" through. def <%= "#{singular_table_name}_params" %> <%- if attributes_names.empty? -%> params[:<%= singular_table_name %>] <%- else -%> params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) <%- end -%> end
destroy()
click to toggle source
# File lib/generators/collection_scaffold/templates/controller.rb, line 48 def destroy @<%= orm_instance.destroy %> redirect_to <%= index_helper %>_path, notice: t('action.destroy.successful', model: <%= class_name %>.model_name.human) end # GET <%= route_url %>/collection_edit def collection_edit @collection = <%= class_name %>::Collection.find(params[:ids]) end # GET <%= route_url %>/collection_edit def collection_update @collection = <%= class_name %>::Collection.find(params[:ids]) if @collection.update params[:collection] redirect_to <%= index_helper %>_path, notice: t('action.collection_update.successful', model: <%= class_name %>.model_name.human) else render :collection_edit end end private # Use callbacks to share common setup or constraints between actions. def set_<%= singular_table_name %> @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %> end # Only allow a trusted parameter "white list" through. def <%= "#{singular_table_name}_params" %> <%- if attributes_names.empty? -%> params[:<%= singular_table_name %>] <%- else -%> params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>) <%- end -%> end end
get_data_attributes(attr, options = {})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 61 def get_data_attributes(attr, options = {}) one = object.is_a?(RecordCollection::Base) ? object.one? : true attrs = {attribute: attr, one: one} attrs[:disabled] = true if options[:disabled] attrs end
get_optional_classes(attr, options = {})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 39 def get_optional_classes(attr, options = {}) classes = [options[:base_class] || 'optional-input', 'optional-attribute-container', attr, active_class(attr)] classes << 'disabled' if options[:disabled] classes << 'one' if !object.is_a?(RecordCollection::Base) or object.one? classes << 'error' if object.errors[attr].present? classes end
optional_boolean(attr, options = {})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 17 def optional_boolean(attr, options = {}) #classes = get_optional_classes(attr, options.merge(base_class: 'optional-boolean')) #label_tag = label(attr, options[:label]) #check_box_tag = check_box(attr, options) #add_collection_ids @template.content_tag(:div, label_tag + check_box_tag , class: classes, data: get_data_attributes(attr, options)) optional_form_element(:check_box, attr, options.reverse_merge(base_class: 'optional-boolean')) end
optional_form_element(element, attr, options = {})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 47 def optional_form_element(element, attr, options = {}) classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-text-area')) label_tag = label(attr, options[:label]) element_tag = send(element, attr, options) content = label_tag + element_tag # Hint tag is empty to make it easy to implement using js. Adding it as text is a better option than hiding the text. # Graceful Degradation is not an option anyway content += @template.content_tag(:span, nil, class: 'optional-attribute-hint', data: {hint: options[:hint]}) if options[:hint].present? content += @template.content_tag(:span, options[:append_text], class: 'optional-attribute-append') if options[:append_text].present? add_collection_ids @template.content_tag(:div, content, class: classes, data: get_data_attributes(attr, options)) end
optional_input(attr, options = {})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 26 def optional_input(attr, options = {}) classes = get_optional_classes(attr, options.reverse_merge(base_class: 'optional-input')) add_collection_ids @template.content_tag(:div, input(attr, options), class: classes, data: get_data_attributes(attr)) end
optional_text_area(attr, options={})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 35 def optional_text_area(attr, options={}) optional_form_element(:text_area, attr, options.reverse_merge(base_class: 'optional-text-area')) end
optional_text_field(attr, options={})
click to toggle source
# File lib/record_collection/rails/form_builder.rb, line 31 def optional_text_field(attr, options={}) optional_form_element(:text_area, attr, options.reverse_merge(base_class: 'optional-text-field')) end