diff --git a/app/controllers/announcements_controller.rb b/app/controllers/announcements_controller.rb new file mode 100644 --- /dev/null +++ b/app/controllers/announcements_controller.rb @@ -0,0 +1,91 @@ +class AnnouncementsController < ApplicationController + + before_filter :authenticate + before_filter { |controller| controller.authorization_by_roles(['admin'])} + + in_place_edit_for :announcement, :published + + # GET /announcements + # GET /announcements.xml + def index + @announcements = Announcement.find(:all) + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @announcements } + end + end + + # GET /announcements/1 + # GET /announcements/1.xml + def show + @announcement = Announcement.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @announcement } + end + end + + # GET /announcements/new + # GET /announcements/new.xml + def new + @announcement = Announcement.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @announcement } + end + end + + # GET /announcements/1/edit + def edit + @announcement = Announcement.find(params[:id]) + end + + # POST /announcements + # POST /announcements.xml + def create + @announcement = Announcement.new(params[:announcement]) + + respond_to do |format| + if @announcement.save + flash[:notice] = 'Announcement was successfully created.' + format.html { redirect_to(@announcement) } + format.xml { render :xml => @announcement, :status => :created, :location => @announcement } + else + format.html { render :action => "new" } + format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /announcements/1 + # PUT /announcements/1.xml + def update + @announcement = Announcement.find(params[:id]) + + respond_to do |format| + if @announcement.update_attributes(params[:announcement]) + flash[:notice] = 'Announcement was successfully updated.' + format.html { redirect_to(@announcement) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @announcement.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /announcements/1 + # DELETE /announcements/1.xml + def destroy + @announcement = Announcement.find(params[:id]) + @announcement.destroy + + respond_to do |format| + format.html { redirect_to(announcements_url) } + format.xml { head :ok } + end + end +end diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -104,6 +104,10 @@ @prob_submissions << { :count => 0, :submission => nil } end end + + @announcements = Announcement.find(:all, + :conditions => "published = 1", + :order => "created_at DESC") end end diff --git a/app/helpers/announcements_helper.rb b/app/helpers/announcements_helper.rb new file mode 100644 --- /dev/null +++ b/app/helpers/announcements_helper.rb @@ -0,0 +1,2 @@ +module AnnouncementsHelper +end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -8,6 +8,7 @@ if (user!=nil) and (user.admin?) # admin menu menu_items << "Administrative task: " + append_to menu_items, '[Announcements]', 'announcements', 'index' append_to menu_items, '[Problem admin]', 'problems', 'index' append_to menu_items, '[User admin]', 'user_admin', 'index' append_to menu_items, '[User stat]', 'user_admin', 'user_stat' diff --git a/app/models/announcement.rb b/app/models/announcement.rb new file mode 100644 --- /dev/null +++ b/app/models/announcement.rb @@ -0,0 +1,2 @@ +class Announcement < ActiveRecord::Base +end diff --git a/app/views/announcements/edit.html.erb b/app/views/announcements/edit.html.erb new file mode 100644 --- /dev/null +++ b/app/views/announcements/edit.html.erb @@ -0,0 +1,27 @@ +
+ Body
+ <%= f.text_area :body %>
+
+ Author
+ <%= f.text_field :author %>
+
+ Published
+ <%= f.check_box :published %>
+
+ <%= f.submit "Update" %> +
+<% end %> + +<%= link_to 'Show', @announcement %> | +<%= link_to 'Back', announcements_path %> diff --git a/app/views/announcements/index.html.erb b/app/views/announcements/index.html.erb new file mode 100644 --- /dev/null +++ b/app/views/announcements/index.html.erb @@ -0,0 +1,30 @@ +<% content_for :head do %> + <%= stylesheet_link_tag 'scaffold' %> + <%= javascript_include_tag :defaults %> +<% end %> + +Body | +Author | +Published | +|||
---|---|---|---|---|---|
<%=h announcement.body %> | +<%=h announcement.author %> | +<%= in_place_editor_field :announcement, :published, {}, :rows => 1 %> | +<%= link_to 'Show', announcement %> | +<%= link_to 'Edit', edit_announcement_path(announcement) %> | +<%= link_to 'Destroy', announcement, :confirm => 'Are you sure?', :method => :delete %> | +
+ Body
+ <%= f.text_area :body %>
+
+ Author
+ <%= f.text_field :author %>
+
+ Published
+ <%= f.check_box :published %>
+
+ <%= f.submit "Create" %> +
+<% end %> + +<%= link_to 'Back', announcements_path %> diff --git a/app/views/announcements/show.html.erb b/app/views/announcements/show.html.erb new file mode 100644 --- /dev/null +++ b/app/views/announcements/show.html.erb @@ -0,0 +1,18 @@ ++ Author: + <%=h @announcement.author %> +
+ ++ Body: + <%=h @announcement.body %> +
+ ++ Published: + <%=h @announcement.published %> +
+ + +<%= link_to 'Edit', edit_announcement_path(@announcement) %> | +<%= link_to 'Back', announcements_path %> diff --git a/app/views/main/_announcement.html.haml b/app/views/main/_announcement.html.haml new file mode 100644 --- /dev/null +++ b/app/views/main/_announcement.html.haml @@ -0,0 +1,4 @@ +.announcement + = markdown(announcement.body) + .pub-info + %p= "#{announcement.author} #{announcement.created_at}" diff --git a/app/views/main/list.html.haml b/app/views/main/list.html.haml --- a/app/views/main/list.html.haml +++ b/app/views/main/list.html.haml @@ -1,5 +1,11 @@ = user_title_bar(@user) +- if @announcements.length!=0 + .announcementbox + %span{:class => 'title'} + Announcements + = render :partial => 'announcement', :collection => @announcements + .submitbox = error_messages_for 'submission' = render :partial => 'submission_box' diff --git a/config/routes.rb b/config/routes.rb --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw do |map| + map.resources :announcements + map.resources :sites # The priority is based upon order of creation: first created -> highest priority. diff --git a/db/migrate/030_create_announcements.rb b/db/migrate/030_create_announcements.rb new file mode 100644 --- /dev/null +++ b/db/migrate/030_create_announcements.rb @@ -0,0 +1,15 @@ +class CreateAnnouncements < ActiveRecord::Migration + def self.up + create_table :announcements do |t| + t.string :author + t.text :body + t.boolean :published + + t.timestamps + end + end + + def self.down + drop_table :announcements + end +end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -9,7 +9,15 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 29) do +ActiveRecord::Schema.define(:version => 30) do + + create_table "announcements", :force => true do |t| + t.string "author" + t.text "body" + t.boolean "published" + t.datetime "created_at" + t.datetime "updated_at" + end create_table "configurations", :force => true do |t| t.string "key" diff --git a/public/stylesheets/application.css b/public/stylesheets/application.css --- a/public/stylesheets/application.css +++ b/public/stylesheets/application.css @@ -156,3 +156,38 @@ padding-left: 20px; background: #feeefe; } + +/********************** + [Announcement] +***********************/ + +div.announcementbox { + margin-top: 10px; + margin-bottom: 10px; + background: green; + padding: 2px; +} + +div.announcementbox span.title { + font-weight: bold; + color: white; + padding-left: 10px; +} + +div.announcement { + margin: 2px; + background: white; + padding: 2px; + padding-left: 10px; + padding-right: 10px; +} + +div.announcement p { + font-size: 13px; +} + +div.pub-info, div.pub-info p { + text-align: right; + font-style: italic; + font-size: 10px; +} \ No newline at end of file diff --git a/test/fixtures/announcements.yml b/test/fixtures/announcements.yml new file mode 100644 --- /dev/null +++ b/test/fixtures/announcements.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +one: + author: MyString + body: MyText + published: false + +two: + author: MyString + body: MyText + published: false diff --git a/test/functional/announcements_controller_test.rb b/test/functional/announcements_controller_test.rb new file mode 100644 --- /dev/null +++ b/test/functional/announcements_controller_test.rb @@ -0,0 +1,45 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class AnnouncementsControllerTest < ActionController::TestCase + def test_should_get_index + get :index + assert_response :success + assert_not_nil assigns(:announcements) + end + + def test_should_get_new + get :new + assert_response :success + end + + def test_should_create_announcement + assert_difference('Announcement.count') do + post :create, :announcement => { } + end + + assert_redirected_to announcement_path(assigns(:announcement)) + end + + def test_should_show_announcement + get :show, :id => announcements(:one).id + assert_response :success + end + + def test_should_get_edit + get :edit, :id => announcements(:one).id + assert_response :success + end + + def test_should_update_announcement + put :update, :id => announcements(:one).id, :announcement => { } + assert_redirected_to announcement_path(assigns(:announcement)) + end + + def test_should_destroy_announcement + assert_difference('Announcement.count', -1) do + delete :destroy, :id => announcements(:one).id + end + + assert_redirected_to announcements_path + end +end diff --git a/test/unit/announcement_test.rb b/test/unit/announcement_test.rb new file mode 100644 --- /dev/null +++ b/test/unit/announcement_test.rb @@ -0,0 +1,8 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class AnnouncementTest < ActiveSupport::TestCase + # Replace this with your real tests. + def test_truth + assert true + end +end