Permalink
Browse files

Added reports

  • Loading branch information...
1 parent e4d4643 commit febc4bf5ad6049e4e6c1844d3e1d4439bc8c715d Oliver Searle-Barnes committed Mar 18, 2010
@@ -0,0 +1,44 @@
+class ReportsController < ApplicationController
+ def index
+ @reports = Report.all
+ end
+
+ def show
+ @report = Report.find(params[:id])
+ end
+
+ def new
+ @report = Report.new
+ end
+
+ def create
+ @report = Report.new(params[:report])
+ if @report.save
+ flash[:notice] = "Successfully created report."
+ redirect_to @report
+ else
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @report = Report.find(params[:id])
+ end
+
+ def update
+ @report = Report.find(params[:id])
+ if @report.update_attributes(params[:report])
+ flash[:notice] = "Successfully updated report."
+ redirect_to @report
+ else
+ render :action => 'edit'
+ end
+ end
+
+ def destroy
+ @report = Report.find(params[:id])
+ @report.destroy
+ flash[:notice] = "Successfully destroyed report."
+ redirect_to reports_url
+ end
+end
@@ -0,0 +1,2 @@
+module ReportsHelper
+end
View
@@ -0,0 +1,3 @@
+class Report < ActiveRecord::Base
+ attr_accessible :name, :query, :database_id
+end
@@ -0,0 +1,16 @@
+<% form_for @report do |f| %>
+ <%= f.error_messages %>
+ <p>
+ <%= f.label :name %><br />
+ <%= f.text_field :name %>
+ </p>
+ <p>
+ <%= f.label :query %><br />
+ <%= f.text_field :query %>
+ </p>
+ <p>
+ <%= f.label :database_id %><br />
+ <%= f.text_field :database_id %>
+ </p>
+ <p><%= f.submit "Submit" %></p>
+<% end %>
@@ -0,0 +1,8 @@
+<% title "Edit Report" %>
+
+<%= render :partial => 'form' %>
+
+<p>
+ <%= link_to "Show", @report %> |
+ <%= link_to "View All", reports_path %>
+</p>
@@ -0,0 +1,21 @@
+<% title "Reports" %>
+
+<table>
+ <tr>
+ <th>Name</th>
+ <th>Query</th>
+ <th>Database</th>
+ </tr>
+ <% for report in @reports %>
+ <tr>
+ <td><%=h report.name %></td>
+ <td><%=h report.query %></td>
+ <td><%=h report.database_id %></td>
+ <td><%= link_to "Show", report %></td>
+ <td><%= link_to "Edit", edit_report_path(report) %></td>
+ <td><%= link_to "Destroy", report, :confirm => 'Are you sure?', :method => :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to "New Report", new_report_path %></p>
@@ -0,0 +1,5 @@
+<% title "New Report" %>
+
+<%= render :partial => 'form' %>
+
+<p><%= link_to "Back to List", reports_path %></p>
@@ -0,0 +1,20 @@
+<% title "Report" %>
+
+<p>
+ <strong>Name:</strong>
+ <%=h @report.name %>
+</p>
+<p>
+ <strong>Query:</strong>
+ <%=h @report.query %>
+</p>
+<p>
+ <strong>Database:</strong>
+ <%=h @report.database_id %>
+</p>
+
+<p>
+ <%= link_to "Edit", edit_report_path(@report) %> |
+ <%= link_to "Destroy", @report, :confirm => 'Are you sure?', :method => :delete %> |
+ <%= link_to "View All", reports_path %>
+</p>
View
@@ -1,4 +1,6 @@
ActionController::Routing::Routes.draw do |map|
+ map.resources :reports
+
map.resources :databases
# The priority is based upon order of creation: first created -> highest priority.
@@ -0,0 +1,14 @@
+class CreateReports < ActiveRecord::Migration
+ def self.up
+ create_table :reports do |t|
+ t.string :name
+ t.string :query
+ t.integer :database_id
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :reports
+ end
+end
@@ -0,0 +1,9 @@
+one:
+ name: MyString
+ query: MyString
+ database_id: 1
+
+two:
+ name: MyString
+ query: MyString
+ database_id: 1
@@ -0,0 +1,68 @@
+require 'test_helper'
+
+class ReportsControllerTest < ActionController::TestCase
+ context "index action" do
+ should "render index template" do
+ get :index
+ assert_template 'index'
+ end
+ end
+
+ context "show action" do
+ should "render show template" do
+ get :show, :id => Report.first
+ assert_template 'show'
+ end
+ end
+
+ context "new action" do
+ should "render new template" do
+ get :new
+ assert_template 'new'
+ end
+ end
+
+ context "create action" do
+ should "render new template when model is invalid" do
+ Report.any_instance.stubs(:valid?).returns(false)
+ post :create
+ assert_template 'new'
+ end
+
+ should "redirect when model is valid" do
+ Report.any_instance.stubs(:valid?).returns(true)
+ post :create
+ assert_redirected_to report_url(assigns(:report))
+ end
+ end
+
+ context "edit action" do
+ should "render edit template" do
+ get :edit, :id => Report.first
+ assert_template 'edit'
+ end
+ end
+
+ context "update action" do
+ should "render edit template when model is invalid" do
+ Report.any_instance.stubs(:valid?).returns(false)
+ put :update, :id => Report.first
+ assert_template 'edit'
+ end
+
+ should "redirect when model is valid" do
+ Report.any_instance.stubs(:valid?).returns(true)
+ put :update, :id => Report.first
+ assert_redirected_to report_url(assigns(:report))
+ end
+ end
+
+ context "destroy action" do
+ should "destroy model and redirect to index action" do
+ report = Report.first
+ delete :destroy, :id => report
+ assert_redirected_to reports_url
+ assert !Report.exists?(report.id)
+ end
+ end
+end
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class ReportTest < ActiveSupport::TestCase
+ should "be valid" do
+ assert Report.new.valid?
+ end
+end

0 comments on commit febc4bf

Please sign in to comment.