Class: ActiveRecord::Migration

Inherits:
Object
  • Object
show all
Defined in:
lib/citier4/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#create_citier_view(theclass) ⇒ Object

function for creating views for migrations



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/citier4/core_ext.rb', line 36

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



88
89
90
91
92
93
94
95
96
# File 'lib/citier4/core_ext.rb', line 88

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



68
69
70
71
72
73
74
75
76
# File 'lib/citier4/core_ext.rb', line 68

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



78
79
80
81
82
83
84
85
86
# File 'lib/citier4/core_ext.rb', line 78

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