Module: NestedEditorFor::Builder
- Defined in:
- lib/nested_editor_for/builder.rb
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#template ⇒ Object
readonly
Returns the value of attribute template.
Instance Method Summary collapse
-
#fields_for(method_or_object, *args, &block) ⇒ Object
override: do arrays like attributes.
- #hidden_field(method_or_object, *args) ⇒ Object
- #nested_editor_for(method, *args, &block) ⇒ Object
- #static_field(method, value) ⇒ Object
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
3 4 5 |
# File 'lib/nested_editor_for/builder.rb', line 3 def object @object end |
#template ⇒ Object (readonly)
Returns the value of attribute template.
3 4 5 |
# File 'lib/nested_editor_for/builder.rb', line 3 def template @template end |
Instance Method Details
#fields_for(method_or_object, *args, &block) ⇒ Object
override: do arrays like attributes
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/nested_editor_for/builder.rb', line 8 def fields_for(method_or_object, *args, &block) = args. if @object case method_or_object when String, Symbol object = @object.send method_or_object if object.is_a?(Array) or object.is_a?(ActiveRecord::Relation) name = [:name] || "#{@object_name}[#{method_or_object}_attributes]" return ((0...object.length).collect do |i| template.fields_for("#{name}[#{i}]", object[i], *args, &block) end).join.html_safe else name = [:name] || method_or_object return super(name, object, *args, &block) end end end super(method_or_object, *args, &block) end |
#hidden_field(method_or_object, *args) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/nested_editor_for/builder.rb', line 31 def hidden_field(method_or_object, *args) method = case method_or_object when String, Symbol; method_or_object when Array; obj.first.class.name.tableize.singularize else; obj.class.name.tableize.singularize end = args. [:id] = [:name].parameterize.underscore if [:name] && !.key?(:id) ["data-attr"] = method super(method_or_object, ) end |
#nested_editor_for(method, *args, &block) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/nested_editor_for/builder.rb', line 61 def nested_editor_for(method, *args, &block) raise ArgumentError, "Missing block" unless block_given? template.instance_variable_set "@enable_nested_records", true attr_name = "#{@object_name}[#{method}_attributes]" # for some reason, things break if I make "#{@object_name}[#{object_name.to_s}_attributes]" the 'id' of the table template.content_tag(:div, class: "nested editor") do template.content_tag(:ol, attr: attr_name) do nested_editor_wrapper(method, attr_name, &block) end end end |
#static_field(method, value) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/nested_editor_for/builder.rb', line 47 def static_field(method, value) attr_name = "#{@object_name}[#{method}]" = { 'data-attr' => method, :type => "hidden", :value => value, :attr => attr_name, :name => attr_name } template.tag("input", ) end |