Top Level Namespace

Defined Under Namespace

Modules: ActiveRecord, Citier

Constant Summary collapse

CITIER_DEBUGGING =
(::Rails.env == 'development')

Instance Method Summary collapse

Instance Method Details

#citier_debug(s) ⇒ Object



3
4
5
6
7
# File 'lib/citier.rb', line 3

def citier_debug(s)
  if CITIER_DEBUGGING
    puts "citier -> " + s
  end
end

#create_citier_view(theclass) ⇒ Object

function for creating views for migrations



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
67
68
# File 'lib/citier/core_ext.rb', line 37

def create_citier_view(theclass)  #function for creating views for migrations 
  # flush any column info in memory
  # Loops through and stops once we've cleaned up to our root class.
  # We MUST user Writable as that is the place where changes might reside!
  reset_class = theclass::Writable 
  until reset_class == ActiveRecord::Base
    citier_debug("Resetting column information on #{reset_class}")
    reset_class.reset_column_information
    reset_class = reset_class.superclass
  end

  self_columns = theclass::Writable.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::Writable.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



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/citier/core_ext.rb', line 93

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



70
71
72
73
74
75
76
77
78
# File 'lib/citier/core_ext.rb', line 70

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



80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/citier/core_ext.rb', line 80

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