From 01579ce68dfe5a6a57b6db7c00efe6eb8fed254d Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Tue, 2 Apr 2013 02:43:01 -0400 Subject: [PATCH] Add Alerts controller --- app/assets/javascripts/alerts.js.coffee | 3 + app/assets/stylesheets/alerts.css.scss | 3 + app/assets/stylesheets/scaffolds.css.scss | 69 ++++++++++++++++++ app/controllers/alerts_controller.rb | 83 ++++++++++++++++++++++ app/helpers/alerts_helper.rb | 2 + app/models/alert.rb | 3 + app/views/alerts/_form.html.erb | 32 +++++++++ app/views/alerts/edit.html.erb | 5 ++ app/views/alerts/index.html.erb | 42 +++++++++++ app/views/alerts/new.html.erb | 5 ++ app/views/alerts/show.html.erb | 27 +++++++ config/routes.rb | 3 + db/migrate/20130402064200_create_alerts.rb | 12 ++++ test/fixtures/alerts.yml | 13 ++++ test/functional/alerts_controller_test.rb | 49 +++++++++++++ test/unit/alert_test.rb | 7 ++ test/unit/helpers/alerts_helper_test.rb | 4 ++ 17 files changed, 362 insertions(+) create mode 100644 app/assets/javascripts/alerts.js.coffee create mode 100644 app/assets/stylesheets/alerts.css.scss create mode 100644 app/assets/stylesheets/scaffolds.css.scss create mode 100644 app/controllers/alerts_controller.rb create mode 100644 app/helpers/alerts_helper.rb create mode 100644 app/models/alert.rb create mode 100644 app/views/alerts/_form.html.erb create mode 100644 app/views/alerts/edit.html.erb create mode 100644 app/views/alerts/index.html.erb create mode 100644 app/views/alerts/new.html.erb create mode 100644 app/views/alerts/show.html.erb create mode 100644 db/migrate/20130402064200_create_alerts.rb create mode 100644 test/fixtures/alerts.yml create mode 100644 test/functional/alerts_controller_test.rb create mode 100644 test/unit/alert_test.rb create mode 100644 test/unit/helpers/alerts_helper_test.rb diff --git a/app/assets/javascripts/alerts.js.coffee b/app/assets/javascripts/alerts.js.coffee new file mode 100644 index 0000000..7615679 --- /dev/null +++ b/app/assets/javascripts/alerts.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/assets/stylesheets/alerts.css.scss b/app/assets/stylesheets/alerts.css.scss new file mode 100644 index 0000000..0969a3d --- /dev/null +++ b/app/assets/stylesheets/alerts.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Alerts controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.css.scss b/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 0000000..6ec6a8f --- /dev/null +++ b/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + &:visited { + color: #666; + } + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + ul li { + font-size: 12px; + list-style: square; + } +} diff --git a/app/controllers/alerts_controller.rb b/app/controllers/alerts_controller.rb new file mode 100644 index 0000000..d5bd605 --- /dev/null +++ b/app/controllers/alerts_controller.rb @@ -0,0 +1,83 @@ +class AlertsController < ApplicationController + # GET /alerts + # GET /alerts.json + def index + @alerts = Alert.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @alerts } + end + end + + # GET /alerts/1 + # GET /alerts/1.json + def show + @alert = Alert.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @alert } + end + end + + # GET /alerts/new + # GET /alerts/new.json + def new + @alert = Alert.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @alert } + end + end + + # GET /alerts/1/edit + def edit + @alert = Alert.find(params[:id]) + end + + # POST /alerts + # POST /alerts.json + def create + @alert = Alert.new(params[:alert]) + + respond_to do |format| + if @alert.save + format.html { redirect_to @alert, notice: 'Alert was successfully created.' } + format.json { render json: @alert, status: :created, location: @alert } + else + format.html { render action: "new" } + format.json { render json: @alert.errors, status: :unprocessable_entity } + end + end + end + + # PUT /alerts/1 + # PUT /alerts/1.json + def update + @alert = Alert.find(params[:id]) + + respond_to do |format| + if @alert.update_attributes(params[:alert]) + format.html { redirect_to @alert, notice: 'Alert was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @alert.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /alerts/1 + # DELETE /alerts/1.json + def destroy + @alert = Alert.find(params[:id]) + @alert.destroy + + respond_to do |format| + format.html { redirect_to alerts_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/alerts_helper.rb b/app/helpers/alerts_helper.rb new file mode 100644 index 0000000..20f5b50 --- /dev/null +++ b/app/helpers/alerts_helper.rb @@ -0,0 +1,2 @@ +module AlertsHelper +end diff --git a/app/models/alert.rb b/app/models/alert.rb new file mode 100644 index 0000000..0d9828e --- /dev/null +++ b/app/models/alert.rb @@ -0,0 +1,3 @@ +class Alert < ActiveRecord::Base + attr_accessible :alerted, :course, :department, :user_id +end diff --git a/app/views/alerts/_form.html.erb b/app/views/alerts/_form.html.erb new file mode 100644 index 0000000..d5ed1b0 --- /dev/null +++ b/app/views/alerts/_form.html.erb @@ -0,0 +1,32 @@ +<%= form_for @alert, :html => { :class => 'form-horizontal' } do |f| %> +
+ <%= f.label :user_id, :class => 'control-label' %> +
+ <%= f.number_field :user_id, :class => 'number_field' %> +
+
+
+ <%= f.label :department, :class => 'control-label' %> +
+ <%= f.text_field :department, :class => 'text_field' %> +
+
+
+ <%= f.label :course, :class => 'control-label' %> +
+ <%= f.text_field :course, :class => 'text_field' %> +
+
+
+ <%= f.label :alerted, :class => 'control-label' %> +
+ <%= f.check_box :alerted, :class => 'check_box' %> +
+
+ +
+ <%= f.submit nil, :class => 'btn btn-primary' %> + <%= link_to t('.cancel', :default => t("helpers.links.cancel")), + alerts_path, :class => 'btn' %> +
+<% end %> diff --git a/app/views/alerts/edit.html.erb b/app/views/alerts/edit.html.erb new file mode 100644 index 0000000..3809f2c --- /dev/null +++ b/app/views/alerts/edit.html.erb @@ -0,0 +1,5 @@ +<%- model_class = Alert -%> + +<%= render :partial => 'form' %> diff --git a/app/views/alerts/index.html.erb b/app/views/alerts/index.html.erb new file mode 100644 index 0000000..54b56cc --- /dev/null +++ b/app/views/alerts/index.html.erb @@ -0,0 +1,42 @@ +<%- model_class = Alert -%> + + + + + + + + + + + + + + + <% @alerts.each do |alert| %> + + + + + + + + + + <% end %> + +
<%= model_class.human_attribute_name(:id) %><%= model_class.human_attribute_name(:user_id) %><%= model_class.human_attribute_name(:department) %><%= model_class.human_attribute_name(:course) %><%= model_class.human_attribute_name(:alerted) %><%= model_class.human_attribute_name(:created_at) %><%=t '.actions', :default => t("helpers.actions") %>
<%= link_to alert.id, alert_path(alert) %><%= alert.user_id %><%= alert.department %><%= alert.course %><%= alert.alerted %><%=l alert.created_at %> + <%= link_to t('.edit', :default => t("helpers.links.edit")), + edit_alert_path(alert), :class => 'btn btn-mini' %> + <%= link_to t('.destroy', :default => t("helpers.links.destroy")), + alert_path(alert), + :method => :delete, + :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, + :class => 'btn btn-mini btn-danger' %> +
+ +<%= link_to t('.new', :default => t("helpers.links.new")), + new_alert_path, + :class => 'btn btn-primary' %> diff --git a/app/views/alerts/new.html.erb b/app/views/alerts/new.html.erb new file mode 100644 index 0000000..7153c5a --- /dev/null +++ b/app/views/alerts/new.html.erb @@ -0,0 +1,5 @@ +<%- model_class = Alert -%> + +<%= render :partial => 'form' %> diff --git a/app/views/alerts/show.html.erb b/app/views/alerts/show.html.erb new file mode 100644 index 0000000..86cce4f --- /dev/null +++ b/app/views/alerts/show.html.erb @@ -0,0 +1,27 @@ +<%- model_class = Alert -%> + + +
+
<%= model_class.human_attribute_name(:user_id) %>:
+
<%= @alert.user_id %>
+
<%= model_class.human_attribute_name(:department) %>:
+
<%= @alert.department %>
+
<%= model_class.human_attribute_name(:course) %>:
+
<%= @alert.course %>
+
<%= model_class.human_attribute_name(:alerted) %>:
+
<%= @alert.alerted %>
+
+ +
+ <%= link_to t('.back', :default => t("helpers.links.back")), + alerts_path, :class => 'btn' %> + <%= link_to t('.edit', :default => t("helpers.links.edit")), + edit_alert_path(@alert), :class => 'btn' %> + <%= link_to t('.destroy', :default => t("helpers.links.destroy")), + alert_path(@alert), + :method => 'delete', + :data => { :confirm => t('.confirm', :default => t("helpers.links.confirm", :default => 'Are you sure?')) }, + :class => 'btn btn-danger' %> +
diff --git a/config/routes.rb b/config/routes.rb index e4e7c5e..36062ff 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ MsuCourseAlerter::Application.routes.draw do + resources :alerts + + # The priority is based upon order of creation: # first created -> highest priority. diff --git a/db/migrate/20130402064200_create_alerts.rb b/db/migrate/20130402064200_create_alerts.rb new file mode 100644 index 0000000..6c51082 --- /dev/null +++ b/db/migrate/20130402064200_create_alerts.rb @@ -0,0 +1,12 @@ +class CreateAlerts < ActiveRecord::Migration + def change + create_table :alerts do |t| + t.integer :user_id + t.string :department + t.string :course + t.boolean :alerted + + t.timestamps + end + end +end diff --git a/test/fixtures/alerts.yml b/test/fixtures/alerts.yml new file mode 100644 index 0000000..1fcbefb --- /dev/null +++ b/test/fixtures/alerts.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/Fixtures.html + +one: + user_id: 1 + department: MyString + course: MyString + alerted: false + +two: + user_id: 1 + department: MyString + course: MyString + alerted: false diff --git a/test/functional/alerts_controller_test.rb b/test/functional/alerts_controller_test.rb new file mode 100644 index 0000000..5f146ca --- /dev/null +++ b/test/functional/alerts_controller_test.rb @@ -0,0 +1,49 @@ +require 'test_helper' + +class AlertsControllerTest < ActionController::TestCase + setup do + @alert = alerts(:one) + end + + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:alerts) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create alert" do + assert_difference('Alert.count') do + post :create, alert: { alerted: @alert.alerted, course: @alert.course, department: @alert.department, user_id: @alert.user_id } + end + + assert_redirected_to alert_path(assigns(:alert)) + end + + test "should show alert" do + get :show, id: @alert + assert_response :success + end + + test "should get edit" do + get :edit, id: @alert + assert_response :success + end + + test "should update alert" do + put :update, id: @alert, alert: { alerted: @alert.alerted, course: @alert.course, department: @alert.department, user_id: @alert.user_id } + assert_redirected_to alert_path(assigns(:alert)) + end + + test "should destroy alert" do + assert_difference('Alert.count', -1) do + delete :destroy, id: @alert + end + + assert_redirected_to alerts_path + end +end diff --git a/test/unit/alert_test.rb b/test/unit/alert_test.rb new file mode 100644 index 0000000..b59c65e --- /dev/null +++ b/test/unit/alert_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class AlertTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/unit/helpers/alerts_helper_test.rb b/test/unit/helpers/alerts_helper_test.rb new file mode 100644 index 0000000..1d9079d --- /dev/null +++ b/test/unit/helpers/alerts_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class AlertsHelperTest < ActionView::TestCase +end