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



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
107
# File 'lib/citier4/core_ext.rb', line 78

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_replace_citier_view(theclass) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/citier4/core_ext.rb', line 109

def create_or_replace_citier_view(theclass) 
 # Convienience function for updating or creating views for migrations
 # TODO : ugly code ... (c'est le meme que create_citier, avec juste l'appel à create_or_update )
 # 
 # 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_or_replace_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

#drop_citier_view(theclass) ⇒ Object

function for dropping views for migrations



143
144
145
146
147
148
149
150
151
# File 'lib/citier4/core_ext.rb', line 143

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



153
154
155
156
157
158
159
160
161
162
# File 'lib/citier4/core_ext.rb', line 153

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)
    # si la view est déjà utilisé, impossible de la délété.
    create_citier_view(theclass)
  else
    citier_debug("Error: #{theclass} VIEW doesn't exist.")
  end
end