Class: FormReflex

Inherits:
ApplicationReflex
  • Object
show all
Includes:
Optimism
Defined in:
app/reflexes/form_reflex.rb

Instance Method Summary collapse

Instance Method Details

#addObject

Creates a new, empty association.



82
83
84
# File 'app/reflexes/form_reflex.rb', line 82

def add
  association.build
end

#associationObject

The add button declares what association to add with data-association



62
63
64
65
66
67
68
# File 'app/reflexes/form_reflex.rb', line 62

def association
  a = element.dataset[:association]
  # send is spoopy, make sure the message you're sending is actually an association

  return unless safe?(session[:model], a)

  session[:model].send(a)
end

#current_userObject



57
58
59
# File 'app/reflexes/form_reflex.rb', line 57

def current_user
  controller.current_user
end

#deleteObject

Removes an unsaved association. This for for the use case where the user has added a new association, hasn’t saved the form yet, and wants to remove the un-saved association.



88
89
90
# File 'app/reflexes/form_reflex.rb', line 88

def delete
  association.delete association[element.dataset[:index].to_i]
end

#handle_changeObject



92
93
94
# File 'app/reflexes/form_reflex.rb', line 92

def handle_change
  # stub

end

#safe?(model, thing) ⇒ Boolean

We need to determine if we’re ‘send`ing a safe message to the model We don’t want someone to modify the name of an html element and send ‘destroy!` for example



72
73
74
75
76
77
78
79
# File 'app/reflexes/form_reflex.rb', line 72

def safe?(model, thing)
  safe = false
  safe ||= !model.association(thing).nil? if model.respond_to?('association')
  if model.respond_to? 'attributes'
    safe ||= model.attributes.keys.include?(thing)
  end
  safe
end