Module: Styx::Forms

Defined in:
lib/styx/forms.rb

Instance Method Summary collapse

Instance Method Details

#styx_form_respond_failure(entity, errors) ⇒ Object



13
14
15
16
17
18
# File 'lib/styx/forms.rb', line 13

def styx_form_respond_failure(entity, errors)
  response.content_type = Mime::TEXT

  errors = Hash[*errors.map {|x| [x, nil]}.flatten] if errors.is_a?(Array)
  render :text => {:entity => entity.to_s.underscore.gsub('/', '_'), :messages => errors}.to_json, :status => :unprocessable_entity
end

#styx_form_respond_success(data, entity = nil, &block) ⇒ Object



20
21
22
23
24
25
# File 'lib/styx/forms.rb', line 20

def styx_form_respond_success(data, entity=nil, &block)
  response.content_type = Mime::TEXT

  block.call(entity) if block_given?
  render :text => (data.is_a?(Proc) ? data.call(entity) : data)
end

#styx_form_store_and_respond(entity, data = nil, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
# File 'lib/styx/forms.rb', line 3

def styx_form_store_and_respond(entity, data=nil, &block)
  response.content_type = Mime::TEXT

  if entity.save
    styx_form_respond_success(data, entity, &block)
  else
    styx_form_respond_failure(entity.class.name, entity.errors.messages)
  end
end