Sha256: 026263e7b80a412c93c777f5858b597a5cbf20121bfd0b3730a2094ac55ed0cb

Contents?: true

Size: 1.7 KB

Versions: 19

Compression:

Stored size: 1.7 KB

Contents

module Enjoy
  module Controllers
    module Contacts
      extend ActiveSupport::Concern
      def index
        @contact_message = model.new
        after_initialize
      end

      def new
        @contact_message = model.new
        after_initialize
      end

      def create
        @contact_message = model.new(message_params)
        after_initialize
        if Enjoy.config.contacts_captcha
          meth = :save_with_captcha
        else
          meth = :save
        end
        if @contact_message.send(meth)
          after_create
          if request.xhr? && process_ajax
            ajax_success
          else
            redirect_after_done
          end
        else
          render_contacts_error
        end
      end

      def sent
      end

      private
      def render_contacts_error
        if request.xhr? && process_ajax
          render json: {errors: @contact_message.errors}, status: 422
        else
          flash.now[:alert] = @contact_message.errors.full_messages.join("\n")
          render action: Enjoy.configuration.recreate_contact_message_action, status: 422
        end
      end
      def process_ajax
        true
      end
      def ajax_success
        render json: {ok: true}
      end
      def redirect_after_done
        redirect_to :contacts_sent
      end
      def after_initialize
        # overrideable hook for updating message
      end
      def after_create
        # overrideable hook for updating message
      end
      def model
        Enjoy::ContactMessage
      end
      def message_params
        params.require(:contact_message).permit(Enjoy.config.contacts_fields.keys + [:name, :email, :phone, :content, :captcha, :captcha_key])
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
enjoy_cms-0.3.5 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.4 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.3 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.2.1 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.2 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.1.2 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.1.1 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.1 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.0.4 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.0.3 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.3.0.2 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.1 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.0.3.2 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.0.3.1 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.0.3 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.0.2 lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.0.1.beta lib/enjoy/controllers/contacts.rb
enjoy_cms-0.2.0.beta lib/enjoy/controllers/contacts.rb
enjoy_cms-0.1.0.beta1 lib/enjoy/controllers/contacts.rb