Class: ActionValidator::FormChannel

Inherits:
ApplicationCable::Channel
  • Object
show all
Includes:
ActionController::RequestForgeryProtection, ActiveSupport::Configurable
Defined in:
app/channels/action_validator/form_channel.rb

Instance Method Summary collapse

Instance Method Details

#subscribedObject



8
9
10
# File 'app/channels/action_validator/form_channel.rb', line 8

def subscribed
  stream_from "action_validator_form_channel"
end

#unsubscribedObject



12
13
14
# File 'app/channels/action_validator/form_channel.rb', line 12

def unsubscribed
  stop_all_streams
end

#validate(data) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/channels/action_validator/form_channel.rb', line 16

def validate(data)
  Rails.logger.tagged("ActionValidator") do
    model_class, params = parse_params(data["formData"])

    if model_class.nil?
      Rails.logger.warn("No valid model class found, cannot perform remote validation.")
      ActionCable.server.broadcast("action_validator_form_channel", { errors: {} })
      return
    end

    instance = validated_instance(model_class, params)

    ActionCable.server.broadcast("action_validator_form_channel", { errors: parse_model_errors(instance) })
  end
end