Module: Apollo::ActiveRecordExtensions::ClassMethods
- Defined in:
- lib/apollo/active_record_extensions.rb
Instance Method Summary collapse
- #current_state_id_column ⇒ Object
- #persist_string_state_name? ⇒ Boolean
- #state_id_to_name(state_id) ⇒ Object
- #state_name_to_id(state_name) ⇒ Object
- #state_set_sql(set_name, options = {}) ⇒ Object
Instance Method Details
#current_state_id_column ⇒ Object
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
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, = {} ) quotes = ([:quotes] or %(')) join = ([:join] or %(,)) surround = ([: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 |