Module: Smooth::UserAdapter

Defined in:
lib/smooth/user_adapter.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
# 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

Instance Method Details

#anonymous?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/smooth/user_adapter.rb', line 39

def anonymous?
  !!(@making_anonymous_request)
end

#generate_token(column) ⇒ Object



10
11
12
13
14
# 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) ⇒ Object



35
36
37
# File 'lib/smooth/user_adapter.rb', line 35

def making_anonymous_request=(setting)
  @making_anonymous_request = !!(setting)
end

#smooth(api = :default) ⇒ Object

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



79
80
81
# File 'lib/smooth/user_adapter.rb', line 79

def smooth(api = :default)
  Smooth.fetch_api(api).as(self)
end

#smooth_authentication_tokenObject



43
44
45
46
# File 'lib/smooth/user_adapter.rb', line 43

def smooth_authentication_token
  read_attribute(:authentication_token)
  "#{ id }:#{ token }"
end