Module: Optimism

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.5.0"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:

  • _self (Optimism)

    the object that the method was called on



20
21
22
# File 'lib/optimism.rb', line 20

def self.configure(&block)
  yield self
end

Instance Method Details

#broadcast_errors(model, attributes, reverse_attributes_for: []) ⇒ Object



24
25
26
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
# File 'lib/optimism.rb', line 24

def broadcast_errors(model, attributes, reverse_attributes_for: [])
  return unless model&.errors&.messages
  resource = ActiveModel::Naming.param_key(model)
  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
  when String, Symbol
    { attributes.to_s => nil }
  when Array
    attributes.flatten.each.with_object({}) { |attr, obj| obj[attr.to_s] = nil }
  else
    raise Exception.new "attributes must be a Hash (Parameters, Indifferent or standard), Array, Symbol or String"
  end
  model.valid? if model.errors.empty?
  process_resource(model, attributes, [resource], reverse_attributes_for: reverse_attributes_for)
  if model.errors.any?
    CableReady::Channels.instance[Optimism.channel_proc[self]].dispatch_event(name: "optimism:form:invalid", detail: {resource: resource}) if Optimism.emit_events
    CableReady::Channels.instance[Optimism.channel_proc[self]].add_css_class(selector: form_selector, name: Optimism.form_class) if Optimism.form_class.present?
    CableReady::Channels.instance[Optimism.channel_proc[self]].set_attribute(selector: submit_selector, name: "disabled") if Optimism.disable_submit
  else
    CableReady::Channels.instance[Optimism.channel_proc[self]].dispatch_event(name: "optimism:form:valid", detail: {resource: resource}) if Optimism.emit_events
    CableReady::Channels.instance[Optimism.channel_proc[self]].remove_css_class(selector: form_selector, name: Optimism.form_class) if Optimism.form_class.present?
    CableReady::Channels.instance[Optimism.channel_proc[self]].remove_attribute(selector: submit_selector, name: "disabled") if Optimism.disable_submit
  end
  CableReady::Channels.instance.broadcast
  head :ok if defined?(head)
end

#process_attribute(model, attribute, ancestry) ⇒ Object



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/optimism.rb', line 72

def process_attribute(model, attribute, ancestry)
  resource = ancestry.shift
  if ancestry.size == 1
    resource += "_#{ancestry.shift}_attributes"
  else
    resource += "_#{ancestry.shift}_attributes_#{ancestry.shift}" until ancestry.empty?
  end
  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.any?
    if attribute.ends_with?("_id") && model.errors.messages.include?(attribute.delete_suffix("_id").to_sym)
      model.errors.messages[attribute.delete_suffix("_id").to_sym].each do |msg|
        model.errors.add(attribute.to_sym, msg)
      end
    end
    if model.errors.messages.map(&:first).include?(attribute.to_sym)
      message = "#{model.errors.full_message(attribute.to_sym, model.errors.messages[attribute.to_sym].first)}#{Optimism.suffix}"
      CableReady::Channels.instance[Optimism.channel_proc[self]].dispatch_event(name: "optimism:attribute:invalid", detail: {resource: resource, attribute: attribute, text: message}) if Optimism.emit_events
      CableReady::Channels.instance[Optimism.channel_proc[self]].add_css_class(selector: container_selector, name: Optimism.error_class) if Optimism.add_css
      CableReady::Channels.instance[Optimism.channel_proc[self]].text_content(selector: error_selector, text: message) if Optimism.inject_inline
    else
      CableReady::Channels.instance[Optimism.channel_proc[self]].dispatch_event(name: "optimism:attribute:valid", detail: {resource: resource, attribute: attribute}) if Optimism.emit_events
      CableReady::Channels.instance[Optimism.channel_proc[self]].remove_css_class(selector: container_selector, name: Optimism.error_class) if Optimism.add_css
      CableReady::Channels.instance[Optimism.channel_proc[self]].text_content(selector: error_selector, text: "") if Optimism.inject_inline
    end
  end
end

#process_resource(model, attributes, ancestry, reverse_attributes_for: []) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/optimism.rb', line 53

def process_resource(model, attributes, ancestry, reverse_attributes_for: [])
  attributes.keys.each do |attribute|
    if attribute.ends_with?("_attributes")
      resource = attribute[0..-12]
      association = model.send(resource.to_sym)
      association = association.reverse if reverse_attributes_for.include?(resource.to_sym)
      if association.respond_to? :each_with_index
        association.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_resource(association, attributes[attribute], ancestry + [resource])
      end
    else
      process_attribute(model, attribute, ancestry.dup)
    end
  end
end