Admin for announcements

This commit is contained in:
Saurabh Bhatia 2014-04-03 11:18:02 +08:00
parent b1585a5e4c
commit be008e4b26
10 changed files with 173 additions and 9 deletions

106
Gemfile.lock Normal file
View File

@ -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!

View File

@ -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.

View File

@ -0,0 +1,4 @@
/*
Place all the styles related to the matching controller here.
They will automatically be included in application.css.
*/

View File

@ -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

View File

@ -0,0 +1,2 @@
module Admin::AnnouncementsHelper
end

View File

@ -1,22 +1,17 @@
class Bulletin class Bulletin
include Mongoid::Document include Mongoid::Document
include Mongoid::Timestamps include Mongoid::Timestamps
include Slug
field :title, type: String, localize: true field :title, type: String, localize: true
field :body, type: String, localize: true field :body, type: String, localize: true
field :uid, type: String field :uid, type: String
# include Slug def generate_slug
def to_param self.generate_slug(self.title, self.uid)
self.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\<~_!]/,'-').gsub(/-{2,}/,'-')+'-'+self.uid end
end
def self.find_by_param(input) def self.find_by_param(input)
self.find_by(uid: input) self.find_by(uid: input)
end end
def generate_uid
self.uid = rand(36**8).to_s(36)
self.save
end
end end

View File

@ -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 %>

View File

@ -1,6 +1,11 @@
Rails.application.routes.draw do Rails.application.routes.draw do
scope "(:locale)", locale: /en|zh_tw/ do scope "(:locale)", locale: /en|zh_tw/ do
namespace :admin do
resources :announcements
end
resources :announcements do resources :announcements do
collection do collection do
get ':uid/:title', to: 'announcements#show', as: :display get ':uid/:title', to: 'announcements#show', as: :display

View File

@ -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

View File

@ -0,0 +1,4 @@
require 'test_helper'
class Admin::AnnouncementsHelperTest < ActionView::TestCase
end