Class: ActiveRecord::Migration
- Inherits:
-
Object
- Object
- ActiveRecord::Migration
- Defined in:
- lib/citier4/core_ext.rb
Instance Method Summary collapse
-
#create_citier_view(theclass) ⇒ Object
function for creating views for migrations.
-
#create_or_update_citier_view(theclass) ⇒ Object
Convienience function for updating or creating views for migrations.
-
#drop_citier_view(theclass) ⇒ Object
function for dropping views for migrations.
-
#update_citier_view(theclass) ⇒ Object
function for updating views for migrations.
Instance Method Details
#create_citier_view(theclass) ⇒ Object
function for creating views for migrations
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 |
# File 'lib/citier4/core_ext.rb', line 76 def create_citier_view(theclass) # flush any column info in memory # Loops through and stops once we've cleaned up to our root class. # We MUST user Writeable as that is the place where changes might reside! reset_class = theclass::Writeable until reset_class == ActiveRecord::Base # Refinery::Core::BaseModel # ActiveRecord::Base eoz citier_debug("Resetting column information on #{reset_class}") reset_class.reset_column_information reset_class = reset_class.superclass end self_columns = theclass::Writeable.column_names.select{ |c| c != "id" } parent_columns = theclass.superclass.column_names.select{ |c| c != "id" } columns = parent_columns+self_columns self_read_table = theclass.table_name self_write_table = theclass::Writeable.table_name parent_read_table = theclass.superclass.table_name # sql = "CREATE VIEW #{self_read_table} AS SELECT #{parent_read_table}.id, #{columns.join(',')} FROM #{parent_read_table}, #{self_write_table} WHERE #{parent_read_table}.id = #{self_write_table}.id" #Use our rails_sql_views gem to create the view so we get it outputted to schema create_view "#{self_read_table}", "SELECT #{parent_read_table}.id, #{columns.join(',')} FROM #{parent_read_table}, #{self_write_table} WHERE #{parent_read_table}.id = #{self_write_table}.id" do |v| v.column :id columns.each do |c| v.column c.to_sym end end # citier_debug("Creating citier view -> #{sql}") #theclass.connection.execute sql end |
#create_or_update_citier_view(theclass) ⇒ Object
Convienience function for updating or creating views for migrations
128 129 130 131 132 133 134 135 136 |
# File 'lib/citier4/core_ext.rb', line 128 def create_or_update_citier_view(theclass) #Convienience function for updating or creating views for migrations citier_debug("Create or Update citier view for #{theclass}") if theclass.table_exists? update_citier_view(theclass) else citier_debug("VIEW DIDN'T EXIST. Now creating for #{theclass}") create_citier_view(theclass) end end |
#drop_citier_view(theclass) ⇒ Object
function for dropping views for migrations
108 109 110 111 112 113 114 115 116 |
# File 'lib/citier4/core_ext.rb', line 108 def drop_citier_view(theclass) #function for dropping views for migrations self_read_table = theclass.table_name # sql = "DROP VIEW #{self_read_table}" drop_view(self_read_table.to_sym) #drop using our rails sql views gem citier_debug("Dropping citier view -> #{sql}") #theclass.connection.execute sql end |
#update_citier_view(theclass) ⇒ Object
function for updating views for migrations
118 119 120 121 122 123 124 125 126 |
# File 'lib/citier4/core_ext.rb', line 118 def update_citier_view(theclass) #function for updating views for migrations citier_debug("Updating citier view for #{theclass}") if theclass.table_exists? drop_citier_view(theclass) create_citier_view(theclass) else citier_debug("Error: #{theclass} VIEW doesn't exist.") end end |