module Hancock::Feedback::Controllers::Contacts

Public Instance Methods

cache_fields?() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 75
def cache_fields?
  ['new', 'index'].include? action_name
end
cache_key() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 78
def cache_key
  'hancock_feedback_contacts_fields'.freeze
end
create() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 26
def create
  @contact_message = model.new(message_params)
  after_initialize

  if Hancock::Feedback.config.captcha
    if Hancock::Feedback.config.recaptcha_support
      if verify_recaptcha
        meth = :save
      else
        meth = :valid?
        @recaptcha_error = I18n.t('hancock.errors.feedback.recaptcha')
      end

    elsif Hancock::Feedback.config.simple_captcha_support
      meth = :save_with_captcha

    else
      meth = :save
    end
  else
    meth = :save
  end

  if @contact_message.send(meth) and @recaptcha_error.blank?
    after_create
    if request.xhr? && process_ajax
      ajax_success
    else
      redirect_after_done
    end
  else
    render_contacts_error
  end
end
fields_partial() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 81
def fields_partial
  "hancock/feedback/contacts/#{(Hancock::Feedback.config.model_settings_support ? 'fields_with_settings' : 'fields')}".freeze
end
hancock_feedback_update_captcha_path() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 69
def hancock_feedback_update_captcha_path
  url_for(action: :update_captcha, time: Time.new.to_i, only_path: true)
end
index() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 14
def index
  @contact_message = model.new
  after_initialize
  render locals: locals unless xhr_checker
end
is_cache_fields() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 72
def is_cache_fields
  cache_fields?
end
locals() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 96
def locals
  {
    is_cache_fields:    is_cache_fields,
    cache_key:          cache_key,
    fields_partial:     fields_partial,
    settings_scope:     settings_scope,
    recaptcha_options:  recaptcha_options
  }
end
new() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 20
def new
  @contact_message = model.new
  after_initialize
  render locals: locals unless xhr_checker
end
recaptcha_options() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 93
def recaptcha_options
  {}
end
sent() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 61
def sent
end
settings_scope() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 84
def settings_scope
  if Hancock::Feedback.config.model_settings_support
    model.settings
  elsif defined?(Settings)
    Settings.ns('feedback')
  else
    nil
  end
end
update_captcha() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 64
def update_captcha
  render layout: false
end

Private Instance Methods

after_create() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 134
def after_create
  # overrideable hook for updating message
end
after_initialize() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 132
def after_initialize
end
ajax_success() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 119
def ajax_success
  render partial: success_partial, locals: locals
  # render json: {ok: true}
end
form_partial() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 137
def form_partial
  "hancock/feedback/contacts/form"
end
message_params() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 146
def message_params
  params.require(model.to_param.gsub("::", "").underscore).permit(
    model.permitted_fields + [:name, :email, :phone, :content, :captcha, :captcha_key]
  )
end
model() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 143
def model
  Hancock::Feedback::ContactMessage
end
process_ajax() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 116
def process_ajax
  true
end
redirect_after_done() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 123
def redirect_after_done
  redirect_to action: :sent
end
render_contacts_error() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 107
def render_contacts_error
  if request.xhr? && process_ajax
    render partial: form_partial, status: 422, locals: locals
    # render json: {errors: @contact_message.errors}, status: 422
  else
    flash.now[:alert] = @contact_message.errors.full_messages.join("\n")
    render action: Hancock::Feedback.configuration.recreate_contact_message_action, status: 422, locals: locals
  end
end
success_partial() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 140
def success_partial
  "hancock/feedback/contacts/success"
end
xhr_checker() click to toggle source
# File lib/hancock/feedback/controllers/contacts.rb, line 126
def xhr_checker
  if request.xhr?
    render layout: false, locals: locals
    return true
  end
end