From f6c6efcdc0f8fbf5c266efff6114f91157ecf127 Mon Sep 17 00:00:00 2001 From: manson Date: Thu, 10 Apr 2014 17:55:24 +0800 Subject: [PATCH] Added category and fix routing --- app/models/category.rb | 11 +++++++++++ app/models/module_app.rb | 2 ++ config/routes.rb | 2 +- lib/orbit_category/categorizable.rb | 19 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 app/models/category.rb create mode 100644 lib/orbit_category/categorizable.rb diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 0000000..a4e94c5 --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,11 @@ +class Category + include Mongoid::Document + include Mongoid::Timestamps + + field :disable, type: Boolean, default: false + field :title, localize: true + + belongs_to :module_app + + scope :enabled, ->{ where(:disable.in => [false, nil, ''])} +end diff --git a/app/models/module_app.rb b/app/models/module_app.rb index e25499c..5bcb83e 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -5,6 +5,8 @@ class ModuleApp field :title, type: String field :key, type: String field :sidebar_order,type: Integer,default: 0 + + has_many :categories, dependent: :destroy, :autosave => true def get_registration OrbitApp::Module::Registration.find_by_key(key) diff --git a/config/routes.rb b/config/routes.rb index 669fd70..c4e2662 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -18,7 +18,7 @@ OrbitStore::Application.routes.draw do # You can have the root of your site routed with "root" root 'pages#home' - locales = Site.find_by(site_active: true).in_use_locales rescue nil + locales = Site.find_by(site_active: true).in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do resources :users diff --git a/lib/orbit_category/categorizable.rb b/lib/orbit_category/categorizable.rb new file mode 100644 index 0000000..a3e1dff --- /dev/null +++ b/lib/orbit_category/categorizable.rb @@ -0,0 +1,19 @@ +module OrbitCategory + module Categorizable + def self.included(base) + base.field :category_id, type: BSON::ObjectId + end + + def category + Category.find(self.category_id) rescue nil + end + + def category=(cate) + self.category_id = Category.find(cate.id).id rescue nil + end + + def disable? + Category.find(self.category_id).disable? rescue nil + end + end +end \ No newline at end of file