Permalink
Browse files

Convert RunQuery to FormModel

  • Loading branch information...
1 parent 11726d6 commit 0f8b89e85eb3aa70985a1e335b46c89a3b382a8a @benjaminoakes benjaminoakes committed Nov 12, 2013
@@ -1,74 +1,21 @@
class RunQueriesController < ApplicationController
- before_action :set_run_query, only: [:show, :edit, :update, :destroy]
-
- # GET /run_queries
- # GET /run_queries.json
- def index
- @run_queries = RunQuery.all
- end
-
- # GET /run_queries/1
- # GET /run_queries/1.json
- def show
- end
-
- # GET /run_queries/new
def new
@run_query = RunQuery.new
end
- # GET /run_queries/1/edit
- def edit
- end
-
- # POST /run_queries
- # POST /run_queries.json
def create
@run_query = RunQuery.new(run_query_params)
- respond_to do |format|
- if @run_query.save
- format.html { redirect_to @run_query, notice: 'Run query was successfully created.' }
- format.json { render action: 'show', status: :created, location: @run_query }
- else
- format.html { render action: 'new' }
- format.json { render json: @run_query.errors, status: :unprocessable_entity }
- end
- end
- end
-
- # PATCH/PUT /run_queries/1
- # PATCH/PUT /run_queries/1.json
- def update
- respond_to do |format|
- if @run_query.update(run_query_params)
- format.html { redirect_to @run_query, notice: 'Run query was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: 'edit' }
- format.json { render json: @run_query.errors, status: :unprocessable_entity }
- end
- end
- end
-
- # DELETE /run_queries/1
- # DELETE /run_queries/1.json
- def destroy
- @run_query.destroy
- respond_to do |format|
- format.html { redirect_to run_queries_url }
- format.json { head :no_content }
+ if @run_query.valid?
+ # Default render
+ else
+ render :action => 'new'
end
end
private
- # Use callbacks to share common setup or constraints between actions.
- def set_run_query
- @run_query = RunQuery.find(params[:id])
- end
- # Never trust parameters from the scary internet, only allow the white list through.
- def run_query_params
- params.require(:run_query).permit(:sql)
- end
+ def run_query_params
+ params.require(:run_query).permit(:sql)
+ end
end
@@ -1,2 +1,12 @@
-class RunQuery < ActiveRecord::Base
+require 'active_model'
+
+class RunQuery
+ include ActiveModel::Model
+
+ attr_reader :sql
+ validates :sql, :presence => true
+
+ def initialize(attrs = {})
+ @sql = attrs[:sql]
+ end
end
@@ -0,0 +1 @@
+Got in create
@@ -1,6 +0,0 @@
-<h1>Editing run_query</h1>
-
-<%= render 'form' %>
-
-<%= link_to 'Show', @run_query %> |
-<%= link_to 'Back', run_queries_path %>
@@ -1,27 +0,0 @@
-<h1>Listing run_queries</h1>
-
-<table>
- <thead>
- <tr>
- <th>Sql</th>
- <th></th>
- <th></th>
- <th></th>
- </tr>
- </thead>
-
- <tbody>
- <% @run_queries.each do |run_query| %>
- <tr>
- <td><%= run_query.sql %></td>
- <td><%= link_to 'Show', run_query %></td>
- <td><%= link_to 'Edit', edit_run_query_path(run_query) %></td>
- <td><%= link_to 'Destroy', run_query, method: :delete, data: { confirm: 'Are you sure?' } %></td>
- </tr>
- <% end %>
- </tbody>
-</table>
-
-<br>
-
-<%= link_to 'New Run query', new_run_query_path %>
@@ -1,4 +0,0 @@
-json.array!(@run_queries) do |run_query|
- json.extract! run_query, :sql
- json.url run_query_url(run_query, format: :json)
-end
@@ -1,9 +0,0 @@
-<p id="notice"><%= notice %></p>
-
-<p>
- <strong>Sql:</strong>
- <%= @run_query.sql %>
-</p>
-
-<%= link_to 'Edit', edit_run_query_path(@run_query) %> |
-<%= link_to 'Back', run_queries_path %>
@@ -1 +0,0 @@
-json.extract! @run_query, :sql, :created_at, :updated_at
@@ -0,0 +1,5 @@
+class DropTableRunQueries < ActiveRecord::Migration
+ def change
+ drop_table :run_queries
+ end
+end
View
@@ -11,13 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20131112214925) do
-
- create_table "run_queries", force: true do |t|
- t.text "sql"
- t.datetime "created_at"
- t.datetime "updated_at"
- end
+ActiveRecord::Schema.define(version: 20131112220255) do
create_table "saved_queries", force: true do |t|
t.string "name"

0 comments on commit 0f8b89e

Please sign in to comment.