Sha256: 83f398c04c9032b53d096555aa4b495c149a993d025f33d9b6092d360fb5bd92

Contents?: true

Size: 1.74 KB

Versions: 5

Compression:

Stored size: 1.74 KB

Contents

module Notifiable
  
  class DeviceTokensController < Notifiable.api_controller_class
    
    rescue_from ActiveRecord::RecordNotFound do |error|
      render :json => {:error => error.message}, :status => :not_found
    end
    
    before_filter :find_device_token, :ensure_authorized!, :except => :create
    
    def create
      @device_token = DeviceToken.find_by(:token => params[:token]) 
      @device_token = DeviceToken.new unless @device_token

      perform_update(device_token_params)
    end
    
    def update
      perform_update(device_token_params)
    end

    def destroy    
      if @device_token.destroy
        head :status => :ok
      else
        render :json => { :errors => @device_token.errors.full_messages }, :status => :unprocessable_entity
      end
    end
    
    private
      def perform_update(params)
        if @device_token.update_attributes(params)
          render :json => @device_token.to_json(:only => Notifiable.api_device_token_params.push(:id)), :status => :ok
        else
          render :json => { :errors => @device_token.errors.full_messages }, :status => :unprocessable_entity
        end
      end
    
      def device_token_params
        device_token_params = params.permit(Notifiable.api_device_token_params)
        
        if current_notifiable_user
          device_token_params[:user_id] = current_notifiable_user.id 
        else
          device_token_params[:user_id] = nil   
        end
        
        device_token_params
      end
    
      def ensure_authorized!
        head :status => :unauthorized if @device_token.user && !@device_token.user.eql?(current_notifiable_user)
      end
    
      def find_device_token
        @device_token = DeviceToken.find(params[:id])
      end
  end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
notifiable-rails-0.18.0 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.17.0 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.16.0 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.15.3 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.15.2 app/controllers/notifiable/device_tokens_controller.rb