diff --git a/Rakefile b/Rakefile index b9ab51c..3a39202 100644 --- a/Rakefile +++ b/Rakefile @@ -3,4 +3,4 @@ require File.expand_path('../config/application', __FILE__) -OrbitStore::Application.load_tasks +Orbit::Application.load_tasks diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 2e8f6d9..4daddc2 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -19,7 +19,7 @@ class PagesController < ApplicationController def home @manifest = @key @dataApi = nil - page = Page.where(:number => 0,:page_id => "").first + page = Page.first OrbitHelper.set_params params OrbitHelper.set_site_locale locale render :html => render_final_page("home",page).html_safe @@ -144,7 +144,7 @@ class PagesController < ApplicationController def get_view page = Page.find(params[:id]) - if page.page_id == "" && page.number == 0 + if page == Page.first @view = File.join(Rails.root, 'app', 'templates', "#{@key}", 'home/index.html.erb') else module_name = page.module.downcase.pluralize @@ -158,6 +158,7 @@ class PagesController < ApplicationController @url = "/#{params[:page][:page_id]}" else page = Page.find(params[:page][:parent_page]) + page.url = page.url.nil? ? "" : page.url @url = page.url + "/#{params[:page][:page_id]}" end p = params.require(:page).permit(:number, :page_id, :module, :parent_page, name_translations: [:en, :zh_tw]) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 2a65d8b..4e2c07d 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -94,6 +94,7 @@ module ApplicationHelper (data[keys[0]].kind_of?(Array) ? data[keys[0]] : data[keys[1]]).each do |item| el = wrap_element.inner_html item.each do |key,value| + value = value.nil? ? "" : value el = el.gsub("{{#{key}}}",value) el = el.gsub("%7B%7B#{key}%7D%7D",value) end @@ -104,6 +105,7 @@ module ApplicationHelper html = doc.to_html if keys[1] (data[keys[1]].kind_of?(Array) ? data[keys[0]] : data[keys[1]]).each do |key,value| + value = value.nil? ? "" : value html = html.gsub("{{#{key}}}",value) html = html.gsub("%7B%7B#{key}%7D%7D",value) end @@ -119,6 +121,7 @@ module ApplicationHelper data = controller.send("#{params[:target_action]}") el = wrap_element_html data.each do |key,value| + value = value.nil? ? "" : value el = el.gsub("{{#{key}}}",value) el = el.gsub("%7B%7B#{key}%7D%7D",value) end diff --git a/app/helpers/pages_helper.rb b/app/helpers/pages_helper.rb index 55942dd..769b685 100644 --- a/app/helpers/pages_helper.rb +++ b/app/helpers/pages_helper.rb @@ -26,6 +26,7 @@ module PagesHelper (data[keys[0]].kind_of?(Array) ? data[keys[0]] : data[keys[1]]).each do |item| el = wrap_element.inner_html item.each do |key,value| + value = value.nil? ? "" : value el = el.gsub("{{#{key}}}",value) el = el.gsub("%7B%7B#{key}%7D%7D",value) end diff --git a/app/models/concerns/slug.rb b/app/models/concerns/slug.rb index 1a005ac..ab361ee 100644 --- a/app/models/concerns/slug.rb +++ b/app/models/concerns/slug.rb @@ -2,16 +2,21 @@ extend ActiveSupport::Concern included do + index({ uid: 1}, { unique: true }) + + validates_uniqueness_of :uid + before_create :generate_uid end def to_param - [self.title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\<~_!]/,'-').gsub(/-{2,}/,'-'), self.uid].join("-") + (self.slug_title.gsub(/[ "'*@#$%^&()+=;:.,?>|\\<~_!:,、。!?;「」〈〉【】/]/,'-')+"-"+self.uid).gsub(/-{2,}/,'-') rescue "-"+self.uid end private def generate_uid - self.uid = rand(36**8).to_s(36) + self.uid = rand(10**8).to_s + generate_uid if self.class.where(:uid=>self.uid).size > 0 end end \ No newline at end of file diff --git a/config/application.rb b/config/application.rb index 66e7975..327dddf 100644 --- a/config/application.rb +++ b/config/application.rb @@ -13,7 +13,7 @@ require "mongoid/railtie" # you've limited to :test, :development, or :production. Bundler.require(:default, Rails.env) -module OrbitStore +module Orbit class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. # Application configuration should go into files in config/initializers diff --git a/config/environment.rb b/config/environment.rb index 535db9b..6b85183 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -2,4 +2,10 @@ require File.expand_path('../application', __FILE__) # Initialize the Rails application. -OrbitStore::Application.initialize! +Orbit::Application.initialize! +if Page.count == 0 + home = Page.new + home.name_translations = {:en=>"home",:zh_tw=>"首頁"} + home.url = "" + home.save +end diff --git a/config/environments/development.rb b/config/environments/development.rb index bae9f7e..4f2b305 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,4 +1,4 @@ -OrbitStore::Application.configure do +Orbit::Application.configure do # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on diff --git a/config/environments/production.rb b/config/environments/production.rb index e1f1e00..ee23d50 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -1,4 +1,4 @@ -OrbitStore::Application.configure do +Orbit::Application.configure do # Settings specified here will take precedence over those in config/application.rb. # Code is not reloaded between requests. diff --git a/config/environments/test.rb b/config/environments/test.rb index a6217e9..318921b 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -1,4 +1,4 @@ -OrbitStore::Application.configure do +Orbit::Application.configure do # Settings specified here will take precedence over those in config/application.rb. # The test environment is used exclusively to run your application's diff --git a/config/initializers/secret_token.rb b/config/initializers/secret_token.rb index 663c805..17572ab 100644 --- a/config/initializers/secret_token.rb +++ b/config/initializers/secret_token.rb @@ -9,4 +9,4 @@ # Make sure your secret_key_base is kept private # if you're sharing your code publicly. -OrbitStore::Application.config.secret_key_base = 'acc6ffc5a7d360c9cf2a7bdb4ddf9a897942ec6767413a5c0324a0fa8b86197a96298288a66bd46d8770d8b6edf509aad65716961c2c364ce006b475e6cfd418' +Orbit::Application.config.secret_key_base = 'acc6ffc5a7d360c9cf2a7bdb4ddf9a897942ec6767413a5c0324a0fa8b86197a96298288a66bd46d8770d8b6edf509aad65716961c2c364ce006b475e6cfd418' diff --git a/config/initializers/session_store.rb b/config/initializers/session_store.rb index a4641cf..fc8e8d1 100644 --- a/config/initializers/session_store.rb +++ b/config/initializers/session_store.rb @@ -1,3 +1,3 @@ # Be sure to restart your server when you modify this file. -OrbitStore::Application.config.session_store :cookie_store, key: '_orbit_store_session' +Orbit::Application.config.session_store :cookie_store, key: '_orbit_store_session' diff --git a/config/routes.rb b/config/routes.rb index c4e2662..80d0bc1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,4 @@ -OrbitStore::Application.routes.draw do +Orbit::Application.routes.draw do get "/pages/edit_view" => "pages#edit_view" get "/pages/preview" => "pages#preview"