Sha256: 01ea54f1ac00fb944f1e41f557785acdc6b11db61218afd68cfa9de049b3d8b4

Contents?: true

Size: 1.41 KB

Versions: 4

Compression:

Stored size: 1.41 KB

Contents

module Notifiable
  
  class DeviceTokensController < Notifiable.api_controller_class
    
    before_filter :ensure_current_notifiable_user, :only => :destroy
    
    def create
      if params[:device_id]
        @device_token = DeviceToken.find_by(:device_id => params[:device_id])
      else
        @device_token = DeviceToken.find_by(:token => params[:token]) 
      end      
      @device_token = DeviceToken.new unless @device_token

      notifiable_params = params.permit(Notifiable.api_device_token_params)
      notifiable_params[:user_id] = current_notifiable_user.id if current_notifiable_user

      if @device_token.update_attributes(notifiable_params)
        head :status => :ok
      else
        render :json => { :errors => @device_token.errors.full_messages }, :status => :internal_server_error
      end
    end

    def destroy
      @device_token = DeviceToken.where(:token => params[:token]).first
      if !@device_token
        head :status => :not_found        
      elsif !@device_token.user.eql?(current_notifiable_user)
        head :status => :unauthorized
      elsif @device_token.destroy
        head :status => :ok
      else
        render :json => { :errors => @device_token.errors.full_messages }, :status => :internal_server_error
      end
    end
    
    private
    def ensure_current_notifiable_user
      head :status => :not_acceptable unless current_notifiable_user
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
notifiable-rails-0.7.0 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.6.1 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.6.0 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.5.1 app/controllers/notifiable/device_tokens_controller.rb