lib/yorisoi.rb in yorisoi-0.0.1 vs lib/yorisoi.rb in yorisoi-0.0.2

- old
+ new

@@ -37,19 +37,25 @@ class Builder < ::ActionView::Helpers::FormBuilder include ActionView::Helpers::TagHelper using Arralizer using Flipper - attr_accessor :wrapper, :invalid_wrapper, :errors_wrapper, :error_wrapper + attr_accessor :wrapper, :invalid_wrapper, :errors_wrapper, :error_wrapper, :stored_error def initialize(object_name, object, template, options) self.default_tag = options[:builder_tag] || {} + store_error(object) super end + def store_error(object) + @stored_error = object.errors.presence || {} + end + + (field_helpers - [:check_box, :radio_button, :fields_for, :hidden_field, :label]).each do |selector| class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 def #{selector}(attribute, options = {}) wrap_field(attribute, options) do super @@ -93,17 +99,10 @@ def label(attribute, options = {}) wrap_field(attribute, options.merge(no_errors: true)) { super } end - def pick_error(attribute) - return nil if @object.nil? || !(messages = @object.errors.messages[attribute]).present? - - errors_wrapper.(messages.map(&error_wrapper.(attribute)).join.html_safe, attribute) - end - - def radio_button(attribute, label_and_value, options = {}) wrap_field(attribute, options) do if label_and_value.is_a?(Array) @template.radio_button(@object_name, attribute, label_and_value.last, objectify_options(options.merge(around: around_proc(label_and_value.first)))) @@ -122,27 +121,53 @@ end.join.html_safe, attribute) end end + def remnant + return if @stored_error.blank? + errors_wrapper.(@stored_error.map { |attribute, *errors| + errors.map { |message| + error_wrapper.(attribute, message) + } + }.flatten.join.html_safe, :remnant) + end + + def select(attribute, choices = nil, options = {}, html_options = {}, &block) wrap_field(attribute, options) { super } end def wrap_field(attribute, options={}) no_errors = options.delete(:no_errors) no_wrap = options.delete(:no_wrap) if (error_html = pick_error(attribute)).present? - no_errors ? yield : yield + error_html + if no_errors + yield + else + delete_stored_error!(attribute) + yield + error_html + end else no_wrap ? yield : wrapper.(yield, attribute) end.html_safe end + def write_error(attribute) + delete_stored_error!(attribute) + pick_error(attribute) + end + + + def delete_stored_error!(attribute) + @stored_error.delete(attribute) + end + + private def around_proc(label_text) ->(tag) { %{<label><span class="label">#{label_text}</span><span class="input">#{tag}</span></label>}.html_safe } @@ -177,10 +202,17 @@ def default_wrapper ->(content, attribute) { content_tag(:div, content, class: %w(normal-field).push(attribute)) } end + def pick_error(attribute) + return nil if @object.nil? || !(messages = @object.errors.messages[attribute]).present? + + errors_wrapper.(messages.map(&error_wrapper.(attribute)).join.html_safe, attribute) + end + + def radio_button_wrapper ->(content, attribute) { content_tag(:ul, content, class: %w(radio-buttons).push(attribute)) } end @@ -216,6 +248,6 @@ tag_generate_errors?(options) ? error_wrapping(supered) : supered end end end end -end +end \ No newline at end of file