Class: ActiveRecord::ConnectionAdapters::PostgreSQLRuleDefinition

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

Overview

Creates a PostgreSQL rule.

The PostgreSQL rule system is basically a query-rewriter. You should take a look at the PostgreSQL documentation for more details, but the basic idea is that a rule can be set to fire on certain query events and will force the query to be rewritten before it is even sent to the query planner and executor.

Generally speaking, you’re probably going to want to stick to create_rule and drop_rule when working with rules.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, event, table, action, commands, options = {}) ⇒ PostgreSQLRuleDefinition

:nodoc:



76
77
78
79
80
81
# File 'lib/active_record/postgresql_extensions/rules.rb', line 76

def initialize(base, name, event, table, action, commands, options = {}) #:nodoc:
  assert_valid_event(event)
  assert_valid_action(action)
  @base, @name, @event, @table, @action, @commands, @options =
    base, name, event, table, action, commands, options
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def action
  @action
end

#baseObject

Returns the value of attribute base.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def base
  @base
end

#commandsObject

Returns the value of attribute commands.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def commands
  @commands
end

#eventObject

Returns the value of attribute event.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def event
  @event
end

#nameObject

Returns the value of attribute name.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def name
  @name
end

#optionsObject

Returns the value of attribute options.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def options
  @options
end

#tableObject

Returns the value of attribute table.



74
75
76
# File 'lib/active_record/postgresql_extensions/rules.rb', line 74

def table
  @table
end

Instance Method Details

#to_sqlObject Also known as: to_s

:nodoc:



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/active_record/postgresql_extensions/rules.rb', line 83

def to_sql #:nodoc:
  sql = 'CREATE '
  sql << ' OR REPLACE ' if options[:force]
  sql << "RULE #{base.quote_rule(name)} AS ON #{event.to_s.upcase} TO #{base.quote_table_name(table)} "
  sql << "WHERE #{options[:conditions] || options[:where]} " if options[:conditions] || options[:where]
  sql << "DO #{action.to_s.upcase} "
  sql << if commands.to_s.upcase == 'NOTHING'
    'NOTHING'
  elsif commands.is_a?(Array)
    '(' << commands.collect(&:to_s).join(';') << ')'
  else
    commands.to_s
  end

  "#{sql};"
end