From 3ca008e041c18e3b564bfbca182b7ae3302db00b Mon Sep 17 00:00:00 2001 From: Saurabh Bhatia Date: Thu, 3 Apr 2014 11:17:07 +0800 Subject: [PATCH] Route Constraint for admin, Slug refactored to a concern --- Gemfile | 4 ++-- .../javascripts/admin/dashboards.js.coffee | 3 +++ .../stylesheets/admin/dashboards.css.scss | 3 +++ app/controllers/admin/dashboards_controller.rb | 4 ++++ app/helpers/admin/dashboards_helper.rb | 2 ++ app/models/concerns/slug.rb | 17 +++++++++-------- app/views/admin/dashboards/index.html.erb | 2 ++ config/initializers/keyword_constraint.rb | 6 ++++++ config/routes.rb | 10 +++++++--- .../admin/dashboards_controller_test.rb | 9 +++++++++ test/helpers/admin/dashboards_helper_test.rb | 4 ++++ 11 files changed, 51 insertions(+), 13 deletions(-) create mode 100644 app/assets/javascripts/admin/dashboards.js.coffee create mode 100644 app/assets/stylesheets/admin/dashboards.css.scss create mode 100644 app/controllers/admin/dashboards_controller.rb create mode 100644 app/helpers/admin/dashboards_helper.rb create mode 100644 app/views/admin/dashboards/index.html.erb create mode 100644 config/initializers/keyword_constraint.rb create mode 100644 test/controllers/admin/dashboards_controller_test.rb create mode 100644 test/helpers/admin/dashboards_helper_test.rb diff --git a/Gemfile b/Gemfile index fdc72f6..f48d47a 100644 --- a/Gemfile +++ b/Gemfile @@ -14,8 +14,8 @@ gem 'uglifier', '>= 1.3.0' # Use CoffeeScript for .js.coffee assets and views gem 'coffee-rails', '~> 4.0.0' -# gem 'announcement', git: 'git@gitlab.tp.rulingcom.com:saurabh/announcement-test.git' -gem 'announcement', path: "/Volumes/MyData/github/orbit4.5/announcement" +gem 'announcement', git: 'git@gitlab.tp.rulingcom.com:saurabh/announcement-test.git' + # See https://github.com/sstephenson/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby diff --git a/app/assets/javascripts/admin/dashboards.js.coffee b/app/assets/javascripts/admin/dashboards.js.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/app/assets/javascripts/admin/dashboards.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://coffeescript.org/ diff --git a/app/assets/stylesheets/admin/dashboards.css.scss b/app/assets/stylesheets/admin/dashboards.css.scss new file mode 100644 index 0000000..aa61d87 --- /dev/null +++ b/app/assets/stylesheets/admin/dashboards.css.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the admin/dashboards controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/admin/dashboards_controller.rb b/app/controllers/admin/dashboards_controller.rb new file mode 100644 index 0000000..bad4c3f --- /dev/null +++ b/app/controllers/admin/dashboards_controller.rb @@ -0,0 +1,4 @@ +class Admin::DashboardsController < ApplicationController + def index + end +end diff --git a/app/helpers/admin/dashboards_helper.rb b/app/helpers/admin/dashboards_helper.rb new file mode 100644 index 0000000..a2644f3 --- /dev/null +++ b/app/helpers/admin/dashboards_helper.rb @@ -0,0 +1,2 @@ +module Admin::DashboardsHelper +end diff --git a/app/models/concerns/slug.rb b/app/models/concerns/slug.rb index 56a0537..1a005ac 100644 --- a/app/models/concerns/slug.rb +++ b/app/models/concerns/slug.rb @@ -1,16 +1,17 @@ module Slug extend ActiveSupport::Concern - def slug - if not respond_to? :title or title.empty? - id - else - I18n.locale - title.parameterize - end + included do + before_create :generate_uid end def to_param - slug + [self.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\<~_!]/,'-').gsub(/-{2,}/,'-'), self.uid].join("-") + end + + private + + def generate_uid + self.uid = rand(36**8).to_s(36) end end \ No newline at end of file diff --git a/app/views/admin/dashboards/index.html.erb b/app/views/admin/dashboards/index.html.erb new file mode 100644 index 0000000..611d39b --- /dev/null +++ b/app/views/admin/dashboards/index.html.erb @@ -0,0 +1,2 @@ +

Admin::Dashboards#index

+

Find me in app/views/admin/dashboards/index.html.erb

diff --git a/config/initializers/keyword_constraint.rb b/config/initializers/keyword_constraint.rb new file mode 100644 index 0000000..6906dd2 --- /dev/null +++ b/config/initializers/keyword_constraint.rb @@ -0,0 +1,6 @@ +class KeywordConstraint + def matches?(request) + keywords = %w{admin member desktop} + keywords.all? { |k| !request.url.include?(k) } + end +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 6bf6d64..adfde71 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,5 +1,9 @@ OrbitStore::Application.routes.draw do + namespace :admin do + get 'dashboards/index' + end + get "/pages/edit_view" => "pages#edit_view" get "/pages/preview" => "pages#preview" @@ -18,8 +22,8 @@ OrbitStore::Application.routes.draw do # You can have the root of your site routed with "root" root 'home#index' scope "(:locale)", locale: /en|zh_tw/ do - get ':page(/:page)(/:page)(/:page)', to: 'pages#show' - resources :pages + get ':page(/:page)(/:page)(/:page)', to: 'pages#show', constraints: KeywordConstraint.new + resources :pages end # Example of regular route: # get 'products/:id' => 'catalog#view' @@ -69,4 +73,4 @@ OrbitStore::Application.routes.draw do # (app/controllers/admin/products_controller.rb) resources :users end -end +end \ No newline at end of file diff --git a/test/controllers/admin/dashboards_controller_test.rb b/test/controllers/admin/dashboards_controller_test.rb new file mode 100644 index 0000000..43d9869 --- /dev/null +++ b/test/controllers/admin/dashboards_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class Admin::DashboardsControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + end + +end diff --git a/test/helpers/admin/dashboards_helper_test.rb b/test/helpers/admin/dashboards_helper_test.rb new file mode 100644 index 0000000..9182de0 --- /dev/null +++ b/test/helpers/admin/dashboards_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class Admin::DashboardsHelperTest < ActionView::TestCase +end