Module: Apollo::ActiveRecordExtensions::ClassMethods

Defined in:
lib/apollo/active_record_extensions.rb

Instance Method Summary collapse

Instance Method Details

#current_state_id_columnObject



72
73
74
# File 'lib/apollo/active_record_extensions.rb', line 72

def current_state_id_column
  self.current_state_column.to_s+"_id"
end

#persist_string_state_name?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/apollo/active_record_extensions.rb', line 56

def persist_string_state_name?
  self.column_names.include?(self.current_state_column)
end

#state_id_to_name(state_id) ⇒ Object



60
61
62
63
64
# File 'lib/apollo/active_record_extensions.rb', line 60

def state_id_to_name(state_id)
  result = ActiveRecord::Base.connection.query "SELECT name FROM states WHERE klass = '#{self.to_s}' AND id = #{state_id}"
  raise "Cannot find state." if result.empty?
  result[0][0]
end

#state_name_to_id(state_name) ⇒ Object



66
67
68
69
70
# File 'lib/apollo/active_record_extensions.rb', line 66

def state_name_to_id(state_name)
  result = ActiveRecord::Base.connection.query "SELECT id FROM states WHERE klass = '#{self.to_s}' AND name = '#{state_name}'"
  raise "Cannot find state" if result.empty?
  result[0][0]
end

#state_set_sql(set_name, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/apollo/active_record_extensions.rb', line 42

def state_set_sql( set_name, options = {} )
  quotes    = (options[:quotes]    or %('))
  join      = (options[:join]      or %(,))
  surround  = (options[:surround]  or ['(',')'])

  str = state_machine.state_sets[set_name.to_sym].state_names
  if persist_string_state_name?
    str = str.collect { |state_name| quotes + state_name + quotes }.join(join)
  else
    str = str.collect { |state_name| state_name_to_id(state_name) }.join(join)
  end
  surround.first + str + surround.last
end