module Smooth::UserAdapter

Public Class Methods

included(base) click to toggle source
# File lib/smooth/user_adapter.rb, line 3
def self.included(base)
  base.extend(ClassMethods)
  base.send(:attr_accessor, :last_request_params, :last_request_headers)

  base.send(:before_create, -> { generate_token(Smooth.config.auth_token_column) })
end

Public Instance Methods

anonymous?() click to toggle source
# File lib/smooth/user_adapter.rb, line 39
def anonymous?
  !!(@making_anonymous_request)
end
generate_token(column) click to toggle source
# File lib/smooth/user_adapter.rb, line 10
def generate_token(column)
  if self.class.column_names.include?(column.to_s)
    write_attribute(column, SecureRandom.urlsafe_base64)
  end
end
making_anonymous_request=(setting) click to toggle source
# File lib/smooth/user_adapter.rb, line 35
def making_anonymous_request=(setting)
  @making_anonymous_request = !!(setting)
end
smooth(api = :default) click to toggle source

Allows for using the current_user making an API request as the source of all queries, and commands run against Smooth resources.

Example:

current_user.smooth.query("books.mine", published_before: 2014)

Piping all queries to the Smooth Resources through the same interface makes implementing a declarative, role based access control policy pretty easy.

You could even add the following methods to all of your ApplicationController

Example:

class ApplicationController < ActionController::Base

def run_query *args, &block
  current_user.smooth.send(:query, *args, &block)
end

def run_command *args, &block
  current_user.smooth.send(:run_command, *args, &block)
end

end

class BooksController < ApplicationController

def index
  render :json => run_query("books", params)
end

end

# File lib/smooth/user_adapter.rb, line 79
def smooth(api = :default)
  Smooth.fetch_api(api).as(self)
end
smooth_authentication_token() click to toggle source
# File lib/smooth/user_adapter.rb, line 43
def smooth_authentication_token
  read_attribute(:authentication_token)
  "#{ id }:#{ token }"
end