From be008e4b260a9beb2890d9964542745e2db4dbe6 Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Thu, 3 Apr 2014 11:18:02 +0800 Subject: [PATCH] Admin for announcements --- Gemfile.lock | 106 ++++++++++++++++++ app/assets/javascripts/admin/announcements.js | 2 + .../stylesheets/admin/announcements.css | 4 + .../admin/announcements_controller.rb | 17 +++ app/helpers/admin/announcements_helper.rb | 2 + app/models/bulletin.rb | 13 +-- app/views/admin/announcements/new.html.erb | 15 +++ config/routes.rb | 5 + .../admin/announcements_controller_test.rb | 14 +++ .../admin/announcements_helper_test.rb | 4 + 10 files changed, 173 insertions(+), 9 deletions(-) create mode 100644 Gemfile.lock create mode 100644 app/assets/javascripts/admin/announcements.js create mode 100644 app/assets/stylesheets/admin/announcements.css create mode 100644 app/controllers/admin/announcements_controller.rb create mode 100644 app/helpers/admin/announcements_helper.rb create mode 100644 app/views/admin/announcements/new.html.erb create mode 100644 test/controllers/admin/announcements_controller_test.rb create mode 100644 test/helpers/admin/announcements_helper_test.rb diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..a240dc8 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,106 @@ +PATH + remote: . + specs: + announcement (0.0.1) + mongoid (= 4.0.0.beta1) + rails (~> 4.1.0.rc2) + +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.1.0.rc2) + actionpack (= 4.1.0.rc2) + actionview (= 4.1.0.rc2) + mail (~> 2.5.4) + actionpack (4.1.0.rc2) + actionview (= 4.1.0.rc2) + activesupport (= 4.1.0.rc2) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + actionview (4.1.0.rc2) + activesupport (= 4.1.0.rc2) + builder (~> 3.1) + erubis (~> 2.7.0) + activemodel (4.1.0.rc2) + activesupport (= 4.1.0.rc2) + builder (~> 3.1) + activerecord (4.1.0.rc2) + activemodel (= 4.1.0.rc2) + activesupport (= 4.1.0.rc2) + arel (~> 5.0.0) + activesupport (4.1.0.rc2) + i18n (~> 0.6, >= 0.6.9) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.1) + tzinfo (~> 1.1) + arel (5.0.0) + atomic (1.1.16) + bson (2.2.1) + builder (3.2.2) + connection_pool (2.0.0) + erubis (2.7.0) + hike (1.2.3) + i18n (0.6.9) + json (1.8.1) + mail (2.5.4) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.25.1) + minitest (5.3.1) + mongoid (4.0.0.beta1) + activemodel (>= 4.0.0) + moped (~> 2.0.beta6) + origin (~> 2.1) + tzinfo (>= 0.3.37) + moped (2.0.0.rc1) + bson (~> 2.2) + connection_pool (~> 2.0) + optionable (~> 0.2.0) + multi_json (1.9.2) + optionable (0.2.0) + origin (2.1.1) + polyglot (0.3.4) + rack (1.5.2) + rack-test (0.6.2) + rack (>= 1.0) + rails (4.1.0.rc2) + actionmailer (= 4.1.0.rc2) + actionpack (= 4.1.0.rc2) + actionview (= 4.1.0.rc2) + activemodel (= 4.1.0.rc2) + activerecord (= 4.1.0.rc2) + activesupport (= 4.1.0.rc2) + bundler (>= 1.3.0, < 2.0) + railties (= 4.1.0.rc2) + sprockets-rails (~> 2.0.0) + railties (4.1.0.rc2) + actionpack (= 4.1.0.rc2) + activesupport (= 4.1.0.rc2) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (10.2.2) + sprockets (2.12.0) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + sprockets-rails (2.0.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (~> 2.8) + thor (0.19.1) + thread_safe (0.3.1) + atomic (>= 1.1.7, < 2) + tilt (1.4.1) + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) + tzinfo (1.1.0) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + announcement! diff --git a/app/assets/javascripts/admin/announcements.js b/app/assets/javascripts/admin/announcements.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/admin/announcements.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/admin/announcements.css b/app/assets/stylesheets/admin/announcements.css new file mode 100644 index 0000000..afad32d --- /dev/null +++ b/app/assets/stylesheets/admin/announcements.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/admin/announcements_controller.rb b/app/controllers/admin/announcements_controller.rb new file mode 100644 index 0000000..96606b6 --- /dev/null +++ b/app/controllers/admin/announcements_controller.rb @@ -0,0 +1,17 @@ +class Admin::AnnouncementsController < ApplicationController + def new + @announcement = Bulletin.new + end + + def create + @announcement = Bulletin.new(bulletin_params) + @announcement.save + end + + private + + # Never trust parameters from the scary internet, only allow the white list through. + def bulletin_params + params.require(:bulletin).permit(title_translations: [:en, :zh_tw], body_translations: [:en, :zh_tw]) + end +end diff --git a/app/helpers/admin/announcements_helper.rb b/app/helpers/admin/announcements_helper.rb new file mode 100644 index 0000000..5973e00 --- /dev/null +++ b/app/helpers/admin/announcements_helper.rb @@ -0,0 +1,2 @@ +module Admin::AnnouncementsHelper +end diff --git a/app/models/bulletin.rb b/app/models/bulletin.rb index 562da55..ae1b349 100644 --- a/app/models/bulletin.rb +++ b/app/models/bulletin.rb @@ -1,22 +1,17 @@ class Bulletin include Mongoid::Document include Mongoid::Timestamps + include Slug field :title, type: String, localize: true field :body, type: String, localize: true field :uid, type: String - # include Slug - def to_param - self.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\<~_!]/,'-').gsub(/-{2,}/,'-')+'-'+self.uid - end + def generate_slug + self.generate_slug(self.title, self.uid) + end def self.find_by_param(input) self.find_by(uid: input) end - - def generate_uid - self.uid = rand(36**8).to_s(36) - self.save - end end diff --git a/app/views/admin/announcements/new.html.erb b/app/views/admin/announcements/new.html.erb new file mode 100644 index 0000000..8b01d33 --- /dev/null +++ b/app/views/admin/announcements/new.html.erb @@ -0,0 +1,15 @@ +<%= form_for @announcement, url: {action: "create"} do |f| %> + <% I18n.available_locales.each do |locale| %> + Title <%= locale.to_s %>: + <%= f.fields_for :title_translations do |n| %> + <%= n.text_field locale %> + <% end %> + + Body <%= locale.to_s %>: + <%= f.fields_for :body_translations do |n| %> + <%= n.text_area locale %> + <% end %> + <% end %> + + <%= f.submit "Create Announcement" %> +<% end %> \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index e5c56a9..5fb352e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,11 @@ Rails.application.routes.draw do scope "(:locale)", locale: /en|zh_tw/ do + + namespace :admin do + resources :announcements + end + resources :announcements do collection do get ':uid/:title', to: 'announcements#show', as: :display diff --git a/test/controllers/admin/announcements_controller_test.rb b/test/controllers/admin/announcements_controller_test.rb new file mode 100644 index 0000000..1bacec2 --- /dev/null +++ b/test/controllers/admin/announcements_controller_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class Admin::AnnouncementsControllerTest < ActionController::TestCase + test "should get new" do + get :new + assert_response :success + end + + test "should get create" do + get :create + assert_response :success + end + +end diff --git a/test/helpers/admin/announcements_helper_test.rb b/test/helpers/admin/announcements_helper_test.rb new file mode 100644 index 0000000..a867b12 --- /dev/null +++ b/test/helpers/admin/announcements_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class Admin::AnnouncementsHelperTest < ActionView::TestCase +end