Skip to content
This repository
Browse code

optionally specify the order in which views are created

This allows views to depend on and include fields from other views.
It is not necessary to specify all the view names, just the ones that
need to be created first
  • Loading branch information...
commit 2e24536f6c333a637f65a04070ec100b2c694d41 1 parent c8bcd63
Robert Wahler authored August 27, 2009 aeden committed August 25, 2010
16  lib/rails_sql_views/schema_dumper.rb
@@ -10,6 +10,12 @@ def self.included(base)
10 10
       # This setting is only used if ActiveRecord::Base.schema_format == :ruby
11 11
       base.cattr_accessor :ignore_views
12 12
       base.ignore_views = []
  13
+      # Optional: specify the order that in which views are created.
  14
+      # This allows views to depend on and include fields from other views.
  15
+      # It is not necessary to specify all the view names, just the ones that 
  16
+      # need to be created first
  17
+      base.cattr_accessor :view_creation_order
  18
+      base.view_creation_order = []
13 19
     end
14 20
     
15 21
     def trailer_with_views(stream)
@@ -36,7 +42,15 @@ def dump_with_views(stream)
36 42
     
37 43
     # Add views to the stream
38 44
     def views(stream)
39  
-      @connection.views.sort.each do |v|
  45
+      if view_creation_order.empty?
  46
+        sorted_views = @connection.views.sort
  47
+      else
  48
+        # set union, merge by joining arrays, removing dups
  49
+        # this will float the view name sin view_creation_order to the top
  50
+        # without requiring all the views to be specified
  51
+        sorted_views = view_creation_order | @connection.views
  52
+      end
  53
+      sorted_views.each do |v|
40 54
         next if [ActiveRecord::Migrator.schema_migrations_table_name, ignore_views].flatten.any? do |ignored|
41 55
           case ignored
42 56
           when String then v == ignored
6  test/schema_dumper_test.rb
@@ -51,6 +51,12 @@ def test_union
51 51
     
52 52
     assert_dump_and_load_succeed
53 53
   end
  54
+  def test_view_creation_order
  55
+    ActiveRecord::SchemaDumper.view_creation_order << :v_people
  56
+    create_people_view
  57
+    assert_dump_and_load_succeed
  58
+    ActiveRecord::SchemaDumper.view_creation_order.pop
  59
+  end
54 60
   def test_symbol_ignore
55 61
     ActiveRecord::SchemaDumper.ignore_views << :v_people
56 62
     create_people_view

0 notes on commit 2e24536

Please sign in to comment.
Something went wrong with that request. Please try again.