Class: ActiveRecord::ConnectionAdapters::PostgreSQLRole

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/postgresql_extensions/roles.rb

Overview

This is a base class for creating and altering ROLEs and is not meant to be used directly.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, action, name, options = {}) ⇒ PostgreSQLRole

:nodoc:



48
49
50
51
52
# File 'lib/active_record/postgresql_extensions/roles.rb', line 48

def initialize(base, action, name, options = {}) #:nodoc:
  assert_valid_action(action)

  @base, @action, @name, @options = base, action, name, options
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



46
47
48
# File 'lib/active_record/postgresql_extensions/roles.rb', line 46

def action
  @action
end

#baseObject

Returns the value of attribute base.



46
47
48
# File 'lib/active_record/postgresql_extensions/roles.rb', line 46

def base
  @base
end

#nameObject

Returns the value of attribute name.



46
47
48
# File 'lib/active_record/postgresql_extensions/roles.rb', line 46

def name
  @name
end

#optionsObject

Returns the value of attribute options.



46
47
48
# File 'lib/active_record/postgresql_extensions/roles.rb', line 46

def options
  @options
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/active_record/postgresql_extensions/roles.rb', line 54

def to_sql #:nodoc:
  sql = Array.new
  if action == :create
    sql << 'CREATE'
  else
    sql << 'ALTER'
  end
  sql << "ROLE #{base.quote_role(name)}"

  if options[:superuser]
    sql << 'SUPERUSER'
  end

  if options[:create_db]
    sql << 'CREATEDB'
  end

  if options[:create_role]
    sql << 'CREATEROLE'
  end

  if options.has_key?(:inherit) && !options[:inherit]
    sql << 'NOINHERIT'
  end

  if options[:login]
    sql << 'LOGIN'
  end

  if options[:connection_limit]
    sql << "CONNECTION LIMIT #{options[:connection_limit].to_i}"
  end

  if options[:password]
    if options.has_key?(:encrypted_password)
      if options[:encrypted_password]
        sql << 'ENCRYPTED'
      else
        sql << 'UNENCRYPTED'
      end
    end

    sql << 'PASSWORD'
    sql << base.quote(options[:password])
  end

  if options[:valid_until]
    sql << 'VALID UNTIL'
    timestamp = case options[:valid_until]
      when Date, Time, DateTime
        options[:valid_until].to_s(:sql)
      else
        options[:valid_until].to_s
    end
    sql << base.quote(timestamp)
  end

  if options[:in_role].present?
    sql << 'IN ROLE'
    sql << Array.wrap(options[:in_role]).collect { |r| base.quote_role(r) }.join(', ')
  end

  if options[:role].present?
    sql << 'ROLE'
    sql << Array.wrap(options[:role]).collect { |r| base.quote_role(r) }.join(', ')
  end

  if options[:admin].present?
    sql << 'ADMIN'
    sql << Array.wrap(options[:admin]).collect { |r| base.quote_role(r) }.join(', ')
  end

  "#{sql.join(' ')};"
end