Przeglądaj źródła

Set tables in controller

Andrew Kane 10 lat temu
rodzic
commit
1338519131

+ 5 - 1
app/controllers/blazer/queries_controller.rb

@@ -13,6 +13,7 @@ module Blazer
 
     before_action :ensure_database_url
     before_action :set_query, only: [:show, :edit, :update, :destroy]
+    before_action :set_tables, only: [:new, :edit]
 
     def index
       @queries = Blazer::Query.order(:name).includes(:creator)
@@ -135,6 +136,10 @@ module Blazer
       @query = Blazer::Query.find(params[:id].to_s.split("-").first)
     end
 
+    def set_tables
+      @tables = tables
+    end
+
     def query_params
       params.require(:query).permit(:name, :description, :statement)
     end
@@ -213,7 +218,6 @@ module Blazer
       rows, error = run_statement(Blazer::Connection.send(:sanitize_sql_array, ["SELECT table_name, column_name, ordinal_position, data_type FROM information_schema.columns WHERE table_schema = ?", schema]))
       Hash[ rows.group_by{|r| r["table_name"] }.map{|t, f| [t, f.sort_by{|f| f["ordinal_position"] }.map{|f| f.slice("column_name", "data_type") }] }.sort_by{|t, f| t } ]
     end
-    helper_method :tables
 
   end
 end

+ 1 - 1
app/views/blazer/queries/_form.html.erb

@@ -16,7 +16,7 @@
           <%= link_to "Back", :back %>
           <span class="text-muted" style="margin-left: 20px;"> Use {start_time} and {end_time} for time ranges</span>
         </div>
-        <%= select_tag :table_names, options_for_select([["Preview table", nil]] + tables.keys), style: "margin-right: 20px; width: 240px;" %>
+        <%= select_tag :table_names, options_for_select([["Preview table", nil]] + @tables.keys), style: "margin-right: 20px; width: 240px;" %>
         <%= link_to "Run", "#", class: "btn btn-info", id: "run", style: "vertical-align: top;" %>
       </div>
     </div>