Chris's first commit at Fri Jan 28 2011 10:14:32 GMT+0800 (CST)

This commit is contained in:
ihower committed 2011-04-13 18:19:51 +08:00
1 parent 554aa0a95d
commit 19a7d51e60
145 files changed
+12742 -893

No files matched your search

+6 -35
View File
@@ -1,41 +1,12 @@
class Asset
include MongoMapper::Document
include Grip::HasAttachment
include Mongoid::Document
key :filename, String, :required => true
key :description, String
mount_uploader :data, AssetUploader
has_grid_attachment :data, :required => true
field :filename
field :description
before_validation :setup_filename
after_save :save_static_file
after_destroy :delete_static_file
validates_presence_of :filename, :data
def url
"/assets/#{self.filename}"
end
def location
"#{RAILS_ROOT}/public/assets/#{self.filename}"
end
protected
def setup_filename
if self.filename.blank? && self.data
self.filename = self.data.file_name
end
end
def save_static_file
f = File.new(self.location, "wb")
f.write(self.data.file.read)
f.close
end
def delete_static_file
File.delete(self.location) rescue nil
end
end
end
+50
View File
@@ -0,0 +1,50 @@
class AttributeAttrModel
include Mongoid::Document
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
field :markup
field :locale, :type => Boolean
field :options, :type => Array
embedded_in :user_attribute_model, :inverse_of => :attribute_attr_models
validates_uniqueness_of :key
# Destroy the i18n_variable
def destroy_i18n_variable
self.i18n_variable.destroy rescue nil
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr)
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class, :parent_id => self.user_attribute_model.i18n_variable_id}))
var.save
self.i18n_variable_id = var.id
end
end
# Convert the string options into an array
def select_options=(var)
self.options = var.gsub(' ', '').split(',')
end
# Convert the array options into a string
def select_options
self.options.to_a.join(', ')
end
# Check if the attribute_attr is set to be destroyed
def should_destroy?
should_destroy.to_i == 1 rescue nil
end
end
+10
View File
@@ -0,0 +1,10 @@
class Home < Page
private
# Remove the validation for parent_id
def validates_presence_of_parent_id?
false
end
end
+13
View File
@@ -0,0 +1,13 @@
class I18nLiquid < Liquid::Tag
def initialize(tag_name, value, tokens)
super
@value = value.to_s
end
def render(context)
I18nVariable.first(:conditions => {:key => @value})[I18n.locale] rescue ''
end
end
Liquid::Template.register_tag('t', I18nLiquid)
+9
View File
@@ -0,0 +1,9 @@
class I18nVariable
include Mongoid::Document
field :key
field :document_class
field :parent_id, :index => true
end
+52 -32
View File
@@ -1,66 +1,86 @@
class Item
include MongoMapper::Document
include Mongoid::Document
key :_type, String
key :name, String, :required => true, :index => true
key :full_name, String, :required => true, :index => true
key :parent_id, ObjectId, :index => true
key_i18n :title, String, :required => true
key :position, Integer, :required => true
key :is_published, Boolean, :required => true, :default => true, :index => true
field :name, :index => true
field :full_name, :index => true
field :i18n_variable_id, :type => BSON::ObjectId
field :parent_id, :index => true
field :parent_name
field :position, :type => Integer
field :is_published, :type => Boolean, :default => true, :index => true
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates :name, :exclusion => { :in => LIST[:forbidden_item_names] }
validates_uniqueness_of :name, :scope => :parent_id
validates_presence_of :name, :full_name, :position, :is_published
validates_presence_of :parent_id, :if => :validates_presence_of_parent_id?
belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
many :children, :class_name => "Item", :foreign_key => :parent_id, :dependent => :destroy
referenced_in :parent, :class_name => "Item"
references_many :children, :class_name => "Item"
before_validation :setup_default_value
attr_accessor :parent_name
def parent_name
@parent_name || self.parent.name
end
def self.find_by_name(item_name)
Item.first(:conditions => { :name => item_name, :is_published => true })
end
# Get an array of ancestors
def ancestors
node, nodes = self, []
nodes << node = node.parent while !node.parent.blank?
nodes << node = node.parent while !node.parent.blank? rescue nil
nodes.reverse
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr) rescue nil
else
var = I18nVariable.new(attr.merge({:key => self.name, :document_class => self.class}))
var.save
self.i18n_variable_id = var.id
end
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
# Check if the page is home
def is_home?
self.name.eql?('home') && self.parent_id.nil?
end
# Build the url from the array of ancestors
def url
urls = ancestors.map{ |a| a.name } << self.name
urls.delete("root")
"/#{urls.join("/")}"
urls.join("/")
end
protected
def setup_default_value
# Set the position value within the parent scope
if self.position.blank?
max_page = Page.last(:order => 'position')
self.position = (max_page)? max_page.position.to_i + 1 : 1
max_page = Item.where(:parent_id => self.parent_id).count
self.position = (max_page)? max_page + 1 : 1
end
if self.parent_name.blank?
self.parent_name = nil
self.parent_id = nil
else
self.parent_id = Item.find_by_name( self.parent_name ).id
end
# Set the parent value
self.parent_name = Item.first(:conditions => {:id => self.parent_id} ).name rescue nil
# Build the full_name from the ancestors array
full_node = self.ancestors.map{ |a| a.name }.push( self.name )
# Remove root node if not root
full_node.shift if full_node.size >= 2
self.full_name = full_node.join("/")
end
# Enable the validation for parent_id
def validates_presence_of_parent_id?
true
end
end
end
+10 -5
View File
@@ -1,14 +1,19 @@
class Layout
include MongoMapper::Document
include Mongoid::Document
key :name, String, :required => true, :index => true
key :description, String
key_i18n :content, String
field :name, :index => true
field :description
field :content
many :children, :class_name => "Item", :foreign_key => "layout_id", :dependent => :nullify
references_many :children, :class_name => "Item"
validates_presence_of :name
validates_format_of :name, :with => /^[0-9a-zA-Z\-_]+$/
validates_uniqueness_of :name
def self.exist_one?
Layout.count > 0
end
end
+5 -3
View File
@@ -1,9 +1,11 @@
class Link < Item
key :url, String, :required => true
field :url
validates_presence_of :url
def link
ApplicationController.helpers.link_to(self.title, self.url)
ApplicationController.helpers.link_to(self.name, self.url)
end
end
end
+8 -6
View File
@@ -1,12 +1,14 @@
class Page < Item
include LayoutSupport
#include LayoutSupport
key_i18n :content, String
key :layout_name, String, :required => true
key :layout_id, ObjectId, :required => true
field :content
field :layout_name
field :layout_id
belongs_to :layout
validates_presence_of :layout_name, :layout_id
referenced_in :layout
protected
@@ -14,7 +16,7 @@ class Page < Item
super
if self.layout_name
self.layout_id = Layout.find_by_name( self.layout_name ).id
self.layout_id = Layout.first(:conditions => {:name => self.layout_name} ).id
end
end
+8
View File
@@ -0,0 +1,8 @@
class Site
include Mongoid::Document
field :in_use_locales, :type => Array
field :valid_locales, :type => Array
end
+33 -34
View File
@@ -1,38 +1,37 @@
class Snippet
include MongoMapper::Document
include Mongoid::Document
key :name, String, :required => true, :index => true
key :full_name, String, :required => true, :index => true
key :parent_id, ObjectId, :required => true, :index => true
key_i18n :content, String
before_validation :setup_default_value
validates_uniqueness_of :name, :scope => :parent_id
belongs_to :parent, :class_name => "Item", :foreign_key => :parent_id
attr_accessor :parent_name
def parent_name
@parent_name || self.parent.name
end
protected
field :name, :index => true
field :full_name, :index => true
field :parent_id, :index => true
field :parent_name
field :content
before_validation :setup_default_value
validates_presence_of :name, :full_name, :parent_id
validates_uniqueness_of :name, :scope => :parent_id
referenced_in :parent, :class_name => "Item", :foreign_key => :parent_id
def ancestors
node, nodes = self, []
nodes << node = node.parent while !node.parent.blank?
nodes.reverse
end
def setup_default_value
self.parent_id = Item.find_by_name( self.parent_name || 'root' ).id
full_node = self.ancestors.map{ |a| a.name }.push( self.name )
full_node.shift if full_node.size >= 2
self.full_name = full_node.join("/")
end
end
# Get an array of ancestors
def ancestors
node, nodes = self, []
nodes << node = node.parent while !node.parent.blank? rescue nil
nodes.reverse
end
def setup_default_value
# Set the parent value
self.parent_name = Item.find( self.parent_id ).name rescue nil
# Build the full_name from the ancestors array
full_node = self.ancestors.map{ |a| a.name }.push( self.name )
# Remove root node if not root
full_node.shift if full_node.size >= 2
self.full_name = full_node.join("/")
end
end
+18 -4
View File
@@ -1,13 +1,27 @@
module SnippetFilter
def render_snippet(snippet_name)
snippet = Snippet.find_by_name(snippet_name)
snippet = Snippet.first(:conditions => {:name => snippet_name})
if snippet
return Liquid::Template.parse( snippet.content ).render
else
return "nothing"
return t('nothing')
end
end
end
def ruling_snippet(snippet_name)
case snippet_name
when 'language_bar'
site = Site.first
@site_in_use_locales = site.in_use_locales
@site_valid_locales = site.valid_locales
end
return Liquid::Template.parse( eval(File.open("#{RAILS_ROOT}/lib/snippets/#{snippet_name}.snippet").read) ).render
end
end
Liquid::Template.register_filter(SnippetFilter)
+46 -10
View File
@@ -1,18 +1,54 @@
class User
include MongoMapper::Document
include Mongoid::Document
devise :authenticatable, :recoverable, :rememberable, :trackable
devise :database_authenticatable, :recoverable, :rememberable, :trackable, :registerable
key :use_attributes, Array
mount_uploader :avatar, AvatarUploader
def method_missing(method)
safe_read(method)
field :admin, :type => Boolean, :default => false
field :active_attributes, :type => Array
embeds_many :user_attributes
before_update :clean_active_attributes
# Update or create the user_attribute records
def user_attributes=(attrs)
attrs.each do |attributes|
if attributes[:id].blank?
user_attributes.build(attributes)
else
user_attribute = user_attributes.detect {|a| a.id.to_s == attributes[:id].to_s }
user_attribute.update_attributes(attributes)
end
end
end
def safe_read(key)
self[key]
rescue MongoMapper::KeyNotFound
return ""
# Get an user_attribute from model key
def get_attribute_from_model_key(key)
self.user_attributes.detect {|a| a.key.to_s == key.to_s }
end
end
# Get the active user_attribute_models
def get_active_attribute_models
self.active_attributes.map{ |attr| get_attribute_model(attr) }
end
# Get an user_attribute_model from key
def get_attribute_model(key)
UserAttributeModel.first(:conditions => {:key => key})
end
# Get the active attributes names or default to '-'
def get_attributes
(self.active_attributes.nil? || self.active_attributes.empty?) ? '-' : self.active_attributes.map{|attr| I18nVariable.first(:conditions => {:key => attr})[I18n.locale] rescue attr}.join(' / ')
end
protected
# Remove empty values
def clean_active_attributes
self.active_attributes.delete('') if self.active_attributes
end
end
+4 -4
View File
@@ -1,9 +1,9 @@
class UserAttribute
include MongoMapper::Document
include Mongoid::Document
key :key, String
key_i18n :name, String
key :attrs, Array
field :key
embedded_in :user, :inverse_of => :user_attributes
end
+59
View File
@@ -0,0 +1,59 @@
class UserAttributeModel
include Mongoid::Document
field :key
field :i18n_variable_id, :type => BSON::ObjectId, :index => true
embeds_many :attribute_attr_models
after_update :destroy_attrs
# Update or create the attribute_attr_model records
def attribute_attr_models=(attrs)
attrs.each do |attributes|
if attributes[:id].blank?
attribute_attr_models.build(attributes)
else
attribute_attr_model = attribute_attr_models.detect {|a| a.id.to_s == attributes[:id].to_s }
attribute_attr_model.update_attributes(attributes)
end
end
end
# Destroy the i18n_variables
def destroy_i18n_variables
self.i18n_variable.destroy rescue nil
self.attribute_attr_models.each do |attr|
attr.destroy_i18n_variable
end
end
# Update or create the i18n_variable record
def i18n_variable=(attr)
if self.i18n_variable_id
self.i18n_variable.update_attributes(attr) rescue nil
else
var = I18nVariable.new(attr.merge({:key => self.key, :document_class => self.class}))
var.save
self.i18n_variable_id = var.id
end
end
# Get the i18n_variable
def i18n_variable
@i18n_variable ||= I18nVariable.find(self.i18n_variable_id) rescue nil
end
protected
# Destroy the attribute_attr_models if marked to destroy
def destroy_attrs
attribute_attr_models.each do |a|
if a.should_destroy?
a.destroy
end
end
end
end