Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
  • 3 commits
  • 7 files changed
  • 0 commit comments
  • 1 contributor
View
5 config/trst.rb
@@ -63,4 +63,9 @@ def self.server
autoload :Public, 'public'
autoload :Utils, 'utils'
autoload :Assets, 'assets'
+ autoload :Book, 'book'
+ autoload :Chapter, 'book'
+ autoload :Page, 'book'
+ autoload :User, 'user'
+ autoload :Task, 'task'
end
View
31 trst/controllers/public.rb
@@ -14,11 +14,36 @@ class Public < Sinatra::Base
end
get '/' do
- haml "Hello"
+ book = Book.where(name: 'trst_pub').first
+ @chapters = book.chapters
+ @page = book.chapters.where(slug: 'home').first
+ haml :page
end
- get '/:page' do |page|
- haml :"#{page}", :layout => !request.xhr?
+ get '/*' do |page|
+ book = Book.where(:name => "trst_pub").first
+ if request.xhr?
+ method, id = params[:splat][0].split('_')
+ @page = Book.send method, id
+ haml :page, :layout => false
+ else
+ #code below just for testing :) real route above
+ @chapters = book.chapters
+ if params[:splat][0] == ""
+ @page = book.chapters.where(:slug => "home").first
+ elsif File.basename(params[:splat][0]) == "index.html"
+ if File.dirname(params[:splat][0]) == "."
+ @page = book.chapters.where(:slug => "home").first
+ else
+ @page = book.chapters.where(:slug => File.dirname(params[:splat][0])).first
+ end
+ else
+ chapter = @chapters.where(:slug => File.dirname(params[:splat][0])).first
+ slug = File.basename(params[:splat][0]).gsub(/(TrustSys-#{chapter.slug.camelize}-)|(.html)/,"")
+ @page = chapter.pages.where(:slug => slug).first
+ end
+ haml :page
+ end
end
##
# You can manage errors like:
View
138 trst/models/book.rb
@@ -0,0 +1,138 @@
+# encoding: utf-8
+module Trst
+ #Book model
+ # Defined fields and default values:
+ # localized_field :name
+ # localized_field :content
+ #Validations:
+ # validates_presence_of :name
+ #Associations:
+ # embeds_many :chapters, :class_name => "Trst::Chapter"
+ class Book
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :name, localize: true
+ field :content, localize: true
+
+ validates_presence_of :name
+
+ embeds_many :chapters, class_name: 'Trst::Chapter'
+
+ class << self
+ # @todo Document this method
+ def page(id)
+ book = where(:'chapters.pages._id' => BSON::ObjectId("#{id}")).first
+ chapter = book.chapters.where(:'pages._id' => BSON::ObjectId("#{id}")).first
+ page = chapter.pages.find(id)
+ end
+ # @todo Document this method
+ def chapter(id)
+ book = where('chapters._id' => BSON::ObjectId("#{id}")).first
+ chapter = book.chapters.find(id)
+ end
+ # @todo Document this method
+ def public_pages
+ book = where(name: "trst_pub").first
+ book.chapters.each_with_object([]) do |chapter,a|
+ chapter.pages.each{|page| a << [page.id.to_s, "#{page.chapter.name}>#{page.name}<"]}
+ end
+ end
+ # @todo Document this method
+ def system_pages
+ book = where(name: "trst_sys").first
+ book.chapters.each_with_object([]) do |chapter,a|
+ chapter.pages.each{|page| a << [page.id.to_s, "#{page.chapter.name}>#{page.name}<"]}
+ end
+ end
+ alias page_related_to system_pages
+ end # Class methods
+ end # Book
+
+ # #Chapter model
+ # Defined fields and default values:
+ # field :order , type: Integer
+ # field :slug, type: String
+ # localized_field :name
+ # localized_field :title
+ # localized_field :content
+ # Validations:
+ # TODO no validations defined
+ # Associations:
+ # embedded_in :book, class_name: 'Trst::Book', inverse_of: :chapters
+ # embeds_many :pages, class_name: 'Trst::Page'
+ class Chapter
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :order, type: Integer
+ field :slug, type: String
+ field :name, localize: true
+ field :title, localize: true
+ field :content, localize: true
+
+ embedded_in :book, class_name: 'Trst::Book', inverse_of: :chapters
+ embeds_many :pages, class_name: 'Trst::Page'
+
+ # @todo Document this method
+ def path
+ slug == 'home' ? '/index.html' : "/#{slug}/index.html"
+ end
+ # @todo
+ def embedded_in
+ chapter
+ end
+ end # Chapter
+
+ # #Page model
+ # Defined fields and default values:
+ # field :order, type: Integer
+ # field :slug, type: String
+ # field :access_lvl, type: Integer, default: 3
+ # field :access_grp, type: Array, default: ['public','admin']
+ # field :task_ids, type: Array, default: []
+ # localized_field :name
+ # localized_field :title
+ # localized_field :content
+ # Validations:
+ # TODO no validations defined
+ # Associations:
+ # embedded_in :chapter, inverse_of: :pages
+ class Page
+ include Mongoid::Document
+ include Mongoid::Timestamps
+
+ field :order, type: Integer
+ field :slug, type: String
+ field :access_lvl, type: Integer, default: 3
+ field :access_grp, type: Array, default: ['public','admin']
+ field :name, localize: true
+ field :title, localize: true
+ field :content, localize: true
+
+ embedded_in :chapter, class_name: 'Trst::Chapter', inverse_of: :pages
+ has_and_belongs_to_many :tasks, class_name: 'Trst::Task', inverse_of: nil
+
+ class << self
+ # @todo
+ def task_ids(id)
+ Trst::Book.page(id).task_ids
+ end
+ end # Class methods
+
+ # @todo Document this method
+ def path
+ retval = chapter.slug == 'home' ? '/' : "/#{chapter.slug}/"
+ retval += "TrustSys-#{chapter.slug.camelize}-#{slug}.html"
+ end
+ # @todo
+ def embedded_in
+ chapter
+ end
+ # @todo Document this method
+ def view_tasks
+ task_ids.each_with_object([]){|id,a| t = tasks.find(id); a << [t.id, t.name]}
+ end
+ end # Page
+
+end # Trst
View
32 trst/models/task.rb
@@ -0,0 +1,32 @@
+# encoding: utf-8
+module Trst
+ class Task
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include ViewHelpers
+ field :name, localize: true
+ field :title, localize: true
+ field :help, localize: true
+ field :haml_path, type: String, default: 'default'
+ field :goal, type: String, default: 'Model.method'
+ field :rels, type: String, default: 'none'
+
+ has_and_belongs_to_many :users, class_name: 'Trst::User', inverse_of: :tasks
+
+ class << self
+ # @todo
+ def user_ids(id)
+ find(id).user_ids
+ end
+ end # Class methods
+
+ # @todo
+ def action
+ goal.split('_').last == 'test' ? 'test' : goal.split('.').last
+ end
+ # @todo
+ def view_users
+ user_ids.each_with_object([]){|id,a| u = users.find(id); a << [u.id, u.login_name]}
+ end
+ end # Task
+end # Trst
View
74 trst/models/user.rb
@@ -0,0 +1,74 @@
+# encoding: utf-8
+module Trst
+ class User
+ include Mongoid::Document
+ include Mongoid::Timestamps
+ include ViewHelpers
+
+ attr_accessor :password, :password_confirmation
+
+ field :login_name, type: String
+ field :name, type: String
+ field :email, type: String
+ field :hashed_password, type: String
+ field :last_login, type: Time
+ field :access_lvl, type: Integer, default: 3
+ field :access_grp, type: Array, default: ['public']
+
+ has_and_belongs_to_many :tasks, class_name: 'Trst::Task', inverse_of: :users
+
+ # Validations
+ validates_uniqueness_of :login_name
+ validates_length_of :email, :within => 10..100
+ validates_uniqueness_of :email, :case_sensitive => false
+ validates_format_of :email, :with => /(\A(\s*)\Z)|(\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z)/i
+ validates_length_of :password, :within => 4..40, :if => :password_required
+
+ before_save :encrypt_password, :if => :password_required
+
+ class << self
+ # Authentication based on name and password
+ # @param [String] login_name User name
+ # @param [String] password User password
+ # @return [User] returns nil if no user with `login_name`, or password
+ # doesn't match
+ def authenticate(login_name, password)
+ current_user = find_by(login_name: login_name) if login_name.present?
+ current_user && current_user.has_password?(password) ? current_user : nil
+ end
+ # @todo
+ def task_ids(id)
+ find(id).task_ids
+ end
+ end # Class methods
+ # Verify `hashed_password`
+ # @return [Boolean]
+ def has_password?(password)
+ ::BCrypt::Password.new(hashed_password) == password
+ end
+ # `true` if `#user` has admin rights
+ # @return [Boolean]
+ def admin?
+ self.access_lvl == -1 || self.access_grp.include?('admin')
+ end
+ # @todo
+ def view_tasks
+ task_ids.each_with_object([]){|id,a| t = tasks.find(id); a << [t.id, t.name]}
+ end
+ protected
+ # Generate encrypted password
+ # @return [String] random string for `#user.salt`
+ def encrypt_password
+ self.hashed_password = ::BCrypt::Password.create(self.password)
+ end
+ # @todo
+ # Document this method
+ def password_required
+ hashed_password.blank? || self.password.present?
+ end
+ # @return [Boolean] `false` just for safety :)
+ def method_missing(m, *args)
+ return false
+ end
+ end
+end
View
9 trst/views/public/layout/_menu.haml
@@ -1,4 +1,9 @@
%ul
- - %w{index services prices contact}.each do |slug|
+ - @chapters.each do |chapter|
%li
- %a{href: "#{slug}.html", title: t("menu.#{slug}")}= t("menu.#{slug}")
+ %a{id: "chapter_#{chapter.id}", href: File.join(lp,chapter.path)}= chapter.name
+ - if chapter.pages.length > 0
+ %ul
+ - chapter.pages.asc(:order).each do |page|
+ %li
+ %a{id: "page_#{page.id}", href: File.join(lp,page.path)}= page.name
View
2 trst/views/public/page.haml
@@ -0,0 +1,2 @@
+:markdown
+ #{@page.content}

No commit comments for this range

Something went wrong with that request. Please try again.