Module: Cocoon::ViewHelpers
- Defined in:
- lib/cocoon/view_helpers.rb
Instance Method Summary collapse
-
#create_object(f, association, force_non_association_create = false) ⇒ Object
creates new association object with its conditions, like “ has_many :admin_comments, class_name: “Comment”, conditions: { author: “Admin” } will create new Comment with author “Admin”.
- #get_partial_path(partial, association) ⇒ Object
-
#link_to_add_association(*args, &block) ⇒ Object
shows a link that will allow to dynamically add a new associated object.
-
#link_to_remove_association(*args, &block) ⇒ Object
this will show a link to remove the current association.
-
#render_association(association, f, new_object, form_name, render_options = {}, custom_partial = nil) ⇒ Object
:nodoc:.
Instance Method Details
#create_object(f, association, force_non_association_create = false) ⇒ Object
creates new association object with its conditions, like “ has_many :admin_comments, class_name: “Comment”, conditions: { author: “Admin” } will create new Comment with author “Admin”
103 104 105 106 107 |
# File 'lib/cocoon/view_helpers.rb', line 103 def create_object(f, association, force_non_association_create=false) assoc = f.object.class.reflect_on_association(association) assoc ? create_object_on_association(f, association, assoc, force_non_association_create) : create_object_on_non_association(f, association) end |
#get_partial_path(partial, association) ⇒ Object
109 110 111 |
# File 'lib/cocoon/view_helpers.rb', line 109 def get_partial_path(partial, association) partial ? partial : association.to_s.singularize + "_fields" end |
#link_to_add_association(*args, &block) ⇒ Object
shows a link that will allow to dynamically add a new associated object.
-
name : the text to show in the link
-
f : the form this should come in (the formtastic form)
-
association : the associated objects, e.g. :tasks, this should be the name of the
has_many
relation. -
html_options: html options to be passed to
link_to
(seelink_to
)- *:render_options* : options passed to `simple_fields_for, semantic_fields_for or fields_for` - *:locals* : the locals hash in the :render_options is handed to the partial - *:partial* : explicitly override the default partial name - *:wrap_object* : a proc that will allow to wrap your object, especially suited when using decorators, or if you want special initialisation - *:form_name* : the parameter for the form in the nested form partial. Default `f`. - *:count* : Count of how many objects will be added on a single click. Default `1`.
-
*&block*: see
link_to
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/cocoon/view_helpers.rb', line 64 def link_to_add_association(*args, &block) if block_given? f = args[0] association = args[1] = args[2] || {} link_to_add_association(capture(&block), f, association, ) else name = args[0] f = args[1] association = args[2] = args[3] || {} = .delete(:render_options) ||= {} override_partial = .delete(:partial) wrap_object = .delete(:wrap_object) force_non_association_create = .delete(:force_non_association_create) || false form_parameter_name = .delete(:form_name) || 'f' count = .delete(:count).to_i [:class] = [[:class], "add_fields"].compact.join(' ') [:'data-association'] = association.to_s.singularize [:'data-associations'] = association.to_s.pluralize new_object = create_object(f, association, force_non_association_create) new_object = wrap_object.call(new_object) if wrap_object.respond_to?(:call) [:'data-association-insertion-template'] = CGI.escapeHTML(render_association(association, f, new_object, form_parameter_name, , override_partial)).html_safe [:'data-count'] = count if count > 0 link_to(name, '#', ) end end |
#link_to_remove_association(*args, &block) ⇒ Object
this will show a link to remove the current association. This should be placed inside the partial. either you give
-
name : the text of the link
-
f : the form this link should be placed in
-
html_options: html options to be passed to link_to (see
link_to
)
or you use the form without name with a *&block*
-
f : the form this link should be placed in
-
html_options: html options to be passed to link_to (see
link_to
) -
*&block*: the output of the block will be show in the link, see
link_to
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/cocoon/view_helpers.rb', line 16 def link_to_remove_association(*args, &block) if block_given? f = args.first = args.second || {} name = capture(&block) link_to_remove_association(name, f, ) else name = args[0] f = args[1] = args[2] || {} is_dynamic = f.object.new_record? classes = [] classes << "remove_fields" classes << (is_dynamic ? 'dynamic' : 'existing') classes << 'destroyed' if f.object.marked_for_destruction? [:class] = [[:class], classes.join(' ')].compact.join(' ') hidden_field_tag("#{f.object_name}[_destroy]", f.object._destroy) + link_to(name, '#', ) end end |
#render_association(association, f, new_object, form_name, render_options = {}, custom_partial = nil) ⇒ Object
:nodoc:
39 40 41 42 43 44 45 46 47 |
# File 'lib/cocoon/view_helpers.rb', line 39 def render_association(association, f, new_object, form_name, ={}, custom_partial=nil) partial = get_partial_path(custom_partial, association) locals = .delete(:locals) || {} method_name = f.respond_to?(:semantic_fields_for) ? :semantic_fields_for : (f.respond_to?(:simple_fields_for) ? :simple_fields_for : :fields_for) f.send(method_name, association, new_object, {:child_index => "new_#{association}"}.merge()) do |builder| = {form_name.to_sym => builder, :dynamic => true}.merge(locals) render(partial, ) end end |