Module: Optimism
- Includes:
- CableReady::Broadcaster
- Included in:
- ActionController::Base
- Defined in:
- lib/optimism.rb,
lib/optimism/railtie.rb,
lib/optimism/version.rb
Defined Under Namespace
Classes: Railtie
Constant Summary collapse
- VERSION =
"0.2.11"
Class Method Summary collapse
Instance Method Summary collapse
- #broadcast_errors(model, attributes) ⇒ Object
- #process_attribute(model, attribute, ancestry) ⇒ Object
- #process_resource(model, attributes, ancestry) ⇒ Object
Class Method Details
.configure {|_self| ... } ⇒ Object
23 24 25 |
# File 'lib/optimism.rb', line 23 def self.configure(&block) yield self end |
Instance Method Details
#broadcast_errors(model, attributes) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/optimism.rb', line 27 def broadcast_errors(model, attributes) return unless model&.errors&. resource = model.class.to_s.downcase form_selector, submit_selector = Optimism.form_selector.sub("RESOURCE", resource), Optimism.submit_selector.sub("RESOURCE", resource) attributes = case attributes when ActionController::Parameters, Hash, ActiveSupport::HashWithIndifferentAccess attributes.to_h.keys when String, Symbol [attributes.to_s] when Array attributes.flatten.map &:to_s else raise Exception.new "attributes must be a Hash (Parameters, Indifferent or standard), Array, Symbol or String" end process_resource(model, attributes, [resource]) if model.errors.any? cable_ready[Optimism.channel].dispatch_event(name: "optimism:form:invalid", detail: {resource: resource}) if Optimism.emit_events cable_ready[Optimism.channel].add_css_class(selector: form_selector, name: Optimism.form_class) if Optimism.form_class.present? cable_ready[Optimism.channel].set_attribute(selector: submit_selector, name: "disabled") if Optimism.disable_submit else cable_ready[Optimism.channel].dispatch_event(name: "optimism:form:valid", detail: {resource: resource}) if Optimism.emit_events cable_ready[Optimism.channel].remove_css_class(selector: form_selector, name: Optimism.form_class) if Optimism.form_class.present? cable_ready[Optimism.channel].remove_attribute(selector: submit_selector, name: "disabled") if Optimism.disable_submit end cable_ready.broadcast head :ok end |
#process_attribute(model, attribute, ancestry) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/optimism.rb', line 69 def process_attribute(model, attribute, ancestry) resource = ancestry.shift resource += "_#{ancestry.shift}_attributes_#{ancestry.shift}" until ancestry.empty? container_selector, error_selector = Optimism.container_selector.sub("RESOURCE", resource).sub("ATTRIBUTE", attribute), Optimism.error_selector.sub("RESOURCE", resource).sub("ATTRIBUTE", attribute) if model.errors..map(&:first).include?(attribute.to_sym) = "#{model.errors.full_message(attribute.humanize, model.errors.messages[attribute.to_sym].first)}#{Optimism.suffix}" cable_ready[Optimism.channel].dispatch_event(name: "optimism:attribute:invalid", detail: {resource: resource, attribute: attribute, text: }) if Optimism.emit_events cable_ready[Optimism.channel].add_css_class(selector: container_selector, name: Optimism.error_class) if Optimism.add_css cable_ready[Optimism.channel].text_content(selector: error_selector, text: ) if Optimism.inject_inline else cable_ready[Optimism.channel].dispatch_event(name: "optimism:attribute:valid", detail: {resource: resource, attribute: attribute}) if Optimism.emit_events cable_ready[Optimism.channel].remove_css_class(selector: container_selector, name: Optimism.error_class) if Optimism.add_css cable_ready[Optimism.channel].text_content(selector: error_selector, text: "") if Optimism.inject_inline end end |
#process_resource(model, attributes, ancestry) ⇒ Object
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/optimism.rb', line 55 def process_resource(model, attributes, ancestry) attributes.each do |attribute| if attribute.ends_with?("_attributes") resource = attribute[0..-12] nested_models = model.send(resource.to_sym) nested_models.each_with_index do |nested, index| process_resource(nested, attributes[attribute][index.to_s], ancestry + [resource, index]) if attributes[attribute].key?(index.to_s) end else process_attribute(model, attribute, ancestry.dup) end end end |