Sha256: 027bf5af2089dde0f86ef35a2087fe746473242a47b0d97689fe1d3233b56b07

Contents?: true

Size: 1.75 KB

Versions: 8

Compression:

Stored size: 1.75 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], :is_valid => true) 
      @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

8 entries across 8 versions & 1 rubygems

Version Path
notifiable-rails-0.19.7 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.6 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.5 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.4 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.3 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.2 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.1 app/controllers/notifiable/device_tokens_controller.rb
notifiable-rails-0.19.0 app/controllers/notifiable/device_tokens_controller.rb