From 664b001e5caa6763010f919ed6b045c958db714e Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Wed, 28 Mar 2012 15:47:20 +0200 Subject: [PATCH] add changes --- README.rdoc | 15 +++++ .../cmtool/application_controller.rb | 2 +- app/models/page.rb | 2 +- app/models/user.rb | 2 +- app/views/cmtool/pages/_form.html.haml | 3 - app/views/pages/404.html.erb | 1 + app/views/pages/home.html.haml | 1 + app/views/pages/show.html.haml | 1 + lib/cmtool.rb | 5 +- lib/cmtool/engine.rb | 1 + lib/cmtool/includes/page.rb | 65 +++++++++++++++++++ lib/cmtool/includes/pages_controller.rb | 33 ++++++++++ lib/cmtool/includes/user.rb | 47 ++++++++++++++ lib/cmtool/page.rb | 65 ------------------- lib/cmtool/user.rb | 45 ------------- .../dummy/app/controllers/pages_controller.rb | 3 + spec/dummy/config/routes.rb | 3 + 17 files changed, 176 insertions(+), 118 deletions(-) create mode 100644 app/views/pages/404.html.erb create mode 100644 app/views/pages/home.html.haml create mode 100644 app/views/pages/show.html.haml create mode 100644 lib/cmtool/includes/page.rb create mode 100644 lib/cmtool/includes/pages_controller.rb create mode 100644 lib/cmtool/includes/user.rb delete mode 100644 lib/cmtool/page.rb delete mode 100644 lib/cmtool/user.rb create mode 100644 spec/dummy/app/controllers/pages_controller.rb diff --git a/README.rdoc b/README.rdoc index 17c4d0b..cb743c7 100644 --- a/README.rdoc +++ b/README.rdoc @@ -53,6 +53,7 @@ The page model allows you to control some interesting things. To create it (app/ # Define the layouts you want to use in your website. Be sure to create them in # app/views/layouts/... + # The first specified layout will become the default def self.layouts [:application, :home, :contact] end @@ -75,3 +76,17 @@ Cmtool will take this value: I18n.locale end end +=== Customize a page +All pages by default are rendered using the view: app/pages/show. So adding and changing this file will +change all displayed pages without their own specific page. To create a specific page, create a page +with the name of the page you created in the Admin section. If for example you have created a page +with the name: about +Then you can give this page a custom look using the view: +app/views/about.html.haml +You can also use erb, slim or your choice of templating engine + +=== Customize your 404 +You can customize what is shown when a page cannot be found. There is a standard view app/pages/404.html.erb with the following content: + <%= render template: 'pages/show' %> +Overwrite this to get another page as show. To controll the content of the 404 page, create a page in the Admin area having the name: 404. +You can make a 404 page for every language in this manner. Here you can also give a custom layout if you do not want to use the default layout. diff --git a/app/controllers/cmtool/application_controller.rb b/app/controllers/cmtool/application_controller.rb index 4c986b7..4917ad9 100644 --- a/app/controllers/cmtool/application_controller.rb +++ b/app/controllers/cmtool/application_controller.rb @@ -6,7 +6,7 @@ module Cmtool def set_locale if respond_to? :cmtool_locale - I18n.locale = :cmtool_locale + I18n.locale = cmtool_locale else I18n.locale = :en end diff --git a/app/models/page.rb b/app/models/page.rb index 0950b05..7955dc6 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -1,3 +1,3 @@ class Page - include Cmtool::Page + include Cmtool::Includes::Page end diff --git a/app/models/user.rb b/app/models/user.rb index 71178af..1bed683 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,3 +1,3 @@ class User - include Cmtool::User + include Cmtool::Includes::User end diff --git a/app/views/cmtool/pages/_form.html.haml b/app/views/cmtool/pages/_form.html.haml index 6987975..93410cc 100644 --- a/app/views/cmtool/pages/_form.html.haml +++ b/app/views/cmtool/pages/_form.html.haml @@ -32,9 +32,6 @@ .field = f.label :parent_id = f.select :parent_id, tree_options_for_select(Page.full_tree(@page.locale), exclude: @page.id, selected: @page.parent_id), include_blank: ' -- ' - .field - = f.label :default - = f.check_box :default .field = f.label :position = f.select :position, (0..20).to_a diff --git a/app/views/pages/404.html.erb b/app/views/pages/404.html.erb new file mode 100644 index 0000000..db28b41 --- /dev/null +++ b/app/views/pages/404.html.erb @@ -0,0 +1 @@ +<%= render template: 'pages/show' %> diff --git a/app/views/pages/home.html.haml b/app/views/pages/home.html.haml new file mode 100644 index 0000000..0b24b2d --- /dev/null +++ b/app/views/pages/home.html.haml @@ -0,0 +1 @@ += render template: 'pages/show' diff --git a/app/views/pages/show.html.haml b/app/views/pages/show.html.haml new file mode 100644 index 0000000..51b14d2 --- /dev/null +++ b/app/views/pages/show.html.haml @@ -0,0 +1 @@ +=raw @page.body diff --git a/lib/cmtool.rb b/lib/cmtool.rb index b6abbe8..2c5a90d 100644 --- a/lib/cmtool.rb +++ b/lib/cmtool.rb @@ -1,6 +1,7 @@ require "cmtool/engine" -require 'cmtool/user' -require 'cmtool/page' +require 'cmtool/includes/user' +require 'cmtool/includes/page' +require 'cmtool/includes/pages_controller' module Cmtool end diff --git a/lib/cmtool/engine.rb b/lib/cmtool/engine.rb index 1972f2f..04631cf 100644 --- a/lib/cmtool/engine.rb +++ b/lib/cmtool/engine.rb @@ -5,6 +5,7 @@ require 'devise' require 'devise_simply_stored' require 'haml-rails' require 'sass-rails' +require 'tinymce-rails' require 'paperclip' require 'jquery-rails' require 'bourbon' diff --git a/lib/cmtool/includes/page.rb b/lib/cmtool/includes/page.rb new file mode 100644 index 0000000..d75c5aa --- /dev/null +++ b/lib/cmtool/includes/page.rb @@ -0,0 +1,65 @@ +module Cmtool + module Includes + module Page + def self.included(klass) + klass.send :include, SimplyStored::Couch + klass.send :include, InstanceMethods + klass.send :extend, ClassMethods + + # PROPERTIES + klass.property :name + klass.property :menu_text + klass.property :title + klass.property :body + klass.property :footer + klass.property :sidebar + klass.property :priority, type: Float, default: 0.5 + klass.property :active, type: :boolean, default: true + klass.property :layout + klass.property :in_menu, type: :boolean + + klass.has_ancestry :by_property => :locale + + klass.validates :name, presence: true + klass.validates :locale, presence: true + + # RELATIONS + klass.has_and_belongs_to_many :keywords, storing_keys: true, class_name: 'Cmtool::Keyword' + + # DEFAULT ORDER + klass.view :all_documents, key: [:position, :name, :title] + klass.view :by_name_and_locale, key: [:name, :locale] + end + + module InstanceMethods + + def generate_name + name = self.class.generate_name(title) + end + end + + module ClassMethods + def generate_name(name) + name.to_s.downcase.gsub(/^\W+/, '').gsub(/\W+$/,'').gsub(/\W+/, '-') + end + def active(*args) + all(*args) + end + + def layouts + [:application, :home, :contact] + end + + def flag_locales + [:ad, :ae, :af, :ag, :ai, :al, :am, :an, :ao, :aq, :ar, :as, :at, :au, :aw, :az, :ba, :bb, :bd, :be, :bf, :bg, :bh, :bi, :bj, :bm, :bn, :bo, :br, :bs, :bt, :bw, :by, :bz, :ca, :cd, :cf, :cg, :ch, :ci, :ck, :cl, :cm, :cn, :co, :cr, :cu, :cv, :cy, :cz, :de, :dj, :dk, :dm, :do, :dz, :ec, :ee, :eg, :eh, :en, :er, :es, :et, :fi, :fj, :fm, :fo, :fr, :ga, :gb, :gd, :ge, :gg, :gh, :gi, :gl, :gm, :gn, :gp, :gq, :gr, :gt, :gu, :gw, :gy, :hk, :hn, :hr, :ht, :hu, :id, :ie, :il, :im, :in, :iq, :ir, :is, :it, :je, :jm, :jo, :jp, :ke, :kg, :kh, :ki, :km, :kn, :kp, :kr, :kw, :ky, :kz, :la, :lb, :lc, :li, :lk, :lr, :ls, :lt, :lu, :lv, :ly, :ma, :mc, :md, :me, :mg, :mh, :mk, :ml, :mm, :mn, :mo, :mq, :mr, :ms, :mt, :mu, :mv, :mw, :mx, :my, :mz, :na, :nc, :ne, :ng, :ni, :nl, :no, :np, :nr, :nz, :om, :pa, :pe, :pf, :pg, :ph, :pk, :pl, :pr, :ps, :pt, :pw, :py, :qa, :re, :ro, :rs, :ru, :rw, :sa, :sb, :sc, :sd, :se, :sg, :si, :sk, :sl, :sm, :sn, :so, :sr, :st, :sv, :sy, :sz, :tc, :td, :tg, :th, :tj, :tl, :tm, :tn, :to, :tr, :tt, :tv, :tw, :tz, :ua, :ug, :us, :uy, :uz, :va, :vc, :ve, :vg, :vi, :vn, :vu, :ws, :ye, :za, :zm, :zw] + end + def locales + [:en, :nl, :de, :es] + end + def default_locale + :en + end + end + end + end +end diff --git a/lib/cmtool/includes/pages_controller.rb b/lib/cmtool/includes/pages_controller.rb new file mode 100644 index 0000000..be6504f --- /dev/null +++ b/lib/cmtool/includes/pages_controller.rb @@ -0,0 +1,33 @@ +module Cmtool + module Includes + module PagesController + def home + page_name = "home" + @page = ::Page.find_by_name_and_locale(page_name, I18n.locale) || ::Page.new(:name => page_name, locale: I18n.locale) + @sub_pages = @page.children.select{|child| child.in_menu.present? } + render :template => "pages/#{page_name}", :layout => @page.layout.presence || ::Page.layouts.first.to_s + end + + # General catcher for pages + def show + @page = ::Page.find_by_name_and_locale(params[:name], I18n.locale) + not_found and return unless @page + + @sub_pages = [@page] + @page.children.select{|child| child.in_menu.present? } + template = "pages/#{@page.name}" + if template_exists?(template) + render :template => template, :layout => @page.layout.presence || ::Page.layouts.first + return + else + render :layout => @page.layout.presence || ::Page.layouts.first + end + end + + def not_found + @page = ::Page.find_by_name_and_locale('404', I18n.locale) || ::Page.new(name: '404', body: "404 Page Not Found") + @sub_pages = [] + render template: 'pages/404', layout: @page.layout.presence || ::Page.layouts.first.to_s, status: 404 + end + end + end +end diff --git a/lib/cmtool/includes/user.rb b/lib/cmtool/includes/user.rb new file mode 100644 index 0000000..c0c60ac --- /dev/null +++ b/lib/cmtool/includes/user.rb @@ -0,0 +1,47 @@ +module Cmtool + module Includes + module User + def self.included(klass) + klass.extend Devise::Models + klass.send :include, SimplyStored::Couch + klass.send :include, Devise::Orm::SimplyStored + klass.send :include, InstanceMethods + + klass.property :employee_email + klass.property :is_admin, type: :boolean + klass.property :active, type: :boolean, default: true + + klass.devise :database_authenticatable, :recoverable, :rememberable, :trackable + + + #== VALIDATIONS + klass.validates_presence_of :email + klass.validates_uniqueness_of :email + klass.validates_presence_of :encrypted_password, if: lambda{ |u| u.email.present? } + klass.validates_confirmation_of :password, if: lambda{ |u| u.password.present? } + + + klass.view :by_email, key: :email + end + module InstanceMethods + def google? + false + end + def name + email + end + + def generated_password + @generated_password + end + + def generate_password! + @password_haystack ||= (:a..:z).to_a + (:A..:Z).to_a + (0..9).to_a + @generated_password = @password_haystack.sample(8).join + self.password = @generated_password + self.password_confirmation = @generated_password + end + end + end + end +end diff --git a/lib/cmtool/page.rb b/lib/cmtool/page.rb deleted file mode 100644 index f7ee013..0000000 --- a/lib/cmtool/page.rb +++ /dev/null @@ -1,65 +0,0 @@ -module Cmtool - module Page - LAYOUTS = %w[application home contact manual] - def self.included(klass) - klass.send :include, SimplyStored::Couch - klass.send :include, InstanceMethods - klass.send :extend, ClassMethods - - # PROPERTIES - klass.property :name - klass.property :menu_text - klass.property :title - klass.property :body - klass.property :footer - klass.property :sidebar - klass.property :priority, type: Float, default: 0.5 - klass.property :default, type: :boolean - klass.property :active, type: :boolean, default: true - klass.property :layout - klass.property :in_menu, type: :boolean - - klass.has_ancestry :by_property => :locale - - klass.validates :name, presence: true - klass.validates :locale, presence: true - - # RELATIONS - klass.has_and_belongs_to_many :keywords, storing_keys: true, class_name: 'Cmtool::Keyword' - - # DEFAULT ORDER - klass.view :all_documents, key: [:position, :name, :title] - klass.view :by_name_and_locale, key: [:name, :locale] - end - - module InstanceMethods - - def generate_name - name = self.class.generate_name(title) - end - end - - module ClassMethods - def generate_name(name) - name.to_s.downcase.gsub(/^\W+/, '').gsub(/\W+$/,'').gsub(/\W+/, '-') - end - def active(*args) - all(*args) - end - - def layouts - [:application, :home, :contact] - end - - def flag_locales - [:ad, :ae, :af, :ag, :ai, :al, :am, :an, :ao, :aq, :ar, :as, :at, :au, :aw, :az, :ba, :bb, :bd, :be, :bf, :bg, :bh, :bi, :bj, :bm, :bn, :bo, :br, :bs, :bt, :bw, :by, :bz, :ca, :cd, :cf, :cg, :ch, :ci, :ck, :cl, :cm, :cn, :co, :cr, :cu, :cv, :cy, :cz, :de, :dj, :dk, :dm, :do, :dz, :ec, :ee, :eg, :eh, :en, :er, :es, :et, :fi, :fj, :fm, :fo, :fr, :ga, :gb, :gd, :ge, :gg, :gh, :gi, :gl, :gm, :gn, :gp, :gq, :gr, :gt, :gu, :gw, :gy, :hk, :hn, :hr, :ht, :hu, :id, :ie, :il, :im, :in, :iq, :ir, :is, :it, :je, :jm, :jo, :jp, :ke, :kg, :kh, :ki, :km, :kn, :kp, :kr, :kw, :ky, :kz, :la, :lb, :lc, :li, :lk, :lr, :ls, :lt, :lu, :lv, :ly, :ma, :mc, :md, :me, :mg, :mh, :mk, :ml, :mm, :mn, :mo, :mq, :mr, :ms, :mt, :mu, :mv, :mw, :mx, :my, :mz, :na, :nc, :ne, :ng, :ni, :nl, :no, :np, :nr, :nz, :om, :pa, :pe, :pf, :pg, :ph, :pk, :pl, :pr, :ps, :pt, :pw, :py, :qa, :re, :ro, :rs, :ru, :rw, :sa, :sb, :sc, :sd, :se, :sg, :si, :sk, :sl, :sm, :sn, :so, :sr, :st, :sv, :sy, :sz, :tc, :td, :tg, :th, :tj, :tl, :tm, :tn, :to, :tr, :tt, :tv, :tw, :tz, :ua, :ug, :us, :uy, :uz, :va, :vc, :ve, :vg, :vi, :vn, :vu, :ws, :ye, :za, :zm, :zw] - end - def locales - [:en, :nl, :de, :es] - end - def default_locale - :en - end - end - end -end diff --git a/lib/cmtool/user.rb b/lib/cmtool/user.rb deleted file mode 100644 index 1242942..0000000 --- a/lib/cmtool/user.rb +++ /dev/null @@ -1,45 +0,0 @@ -module Cmtool - module User - def self.included(klass) - klass.extend Devise::Models - klass.send :include, SimplyStored::Couch - klass.send :include, Devise::Orm::SimplyStored - klass.send :include, InstanceMethods - - klass.property :employee_email - klass.property :is_admin, type: :boolean - klass.property :active, type: :boolean, default: true - - klass.devise :database_authenticatable, :recoverable, :rememberable, :trackable - - - #== VALIDATIONS - klass.validates_presence_of :email - klass.validates_uniqueness_of :email - klass.validates_presence_of :encrypted_password, if: lambda{ |u| u.email.present? } - klass.validates_confirmation_of :password, if: lambda{ |u| u.password.present? } - - - klass.view :by_email, key: :email - end - module InstanceMethods - def google? - false - end - def name - email - end - - def generated_password - @generated_password - end - - def generate_password! - @password_haystack ||= (:a..:z).to_a + (:A..:Z).to_a + (0..9).to_a - @generated_password = @password_haystack.sample(8).join - self.password = @generated_password - self.password_confirmation = @generated_password - end - end - end -end diff --git a/spec/dummy/app/controllers/pages_controller.rb b/spec/dummy/app/controllers/pages_controller.rb new file mode 100644 index 0000000..f81ac73 --- /dev/null +++ b/spec/dummy/app/controllers/pages_controller.rb @@ -0,0 +1,3 @@ +class PagesController < ApplicationController + include Cmtool::Includes::PagesController +end diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index a484e46..5153bc0 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,5 +1,8 @@ Rails.application.routes.draw do devise_for :users, :controllers => {:sessions => 'cmtool/sessions', :passwords => 'cmtool/passwords'} + match "/:name" => "pages#show" + match "/*url" => "pages#not_found" + root :to => 'pages#home' mount Cmtool::Engine => "/cmtool" end