Tag functionalities for new UI

This commit is contained in:
chris 2013-04-23 20:17:35 +08:00
parent ff03d66439
commit bf3d20ea46
25 changed files with 212 additions and 30 deletions

View File

@ -0,0 +1,57 @@
class Admin::ModuleTagsController < OrbitBackendController
before_filter :force_order_for_visitor, only: [:index]
before_filter :force_order_for_user, except: [:index]
before_filter :for_app_sub_manager, except: [:index]
def index
@tags = get_tags
@included_tags = @module_app.module_tags.where(is_default: true)
@default_tags = get_default_tags
end
def new
end
def edit
@tag = Tag.find(params[:id])
end
def create
@tag = @module_app ? @module_app.module_tags.create(params[:tag])
end
def update
@tag = Tag.find(params[:id])
@tag.update_attributes(params[:tag])
end
def destroy
@tag = Tag.find(params[:id])
@tag.destroy
respond_to do |format|
format.js { render 'js/remove_element', :locals => {:id => "#{dom_id @tag}"} }
end
end
def update_included_default
@module_app.update_attribute(:module_tag_ids, (@module_app.module_tag_ids - get_default_tag_ids) << params[:ids])
end
protected
def setup_vars
@module_app = ModuleApp.find(params[:module_app_id]) rescue nil
raise ModuleAppError, 'Can not find ModuleApp' if @module_app.nil?
@module_app_id = @module_app.id rescue nil
end
def get_default_tags
ModuleTag.where(is_default: true)
end
def get_default_tag_ids
get_default_tags.map{|t| t.id}
end
end

View File

@ -1,11 +1,11 @@
class Admin::TagsController < OrbitBackendController class Admin::TagsController < OrbitBackendController
before_filter :force_order_for_visitor,:only=>[:index] before_filter :force_order_for_visitor, only: [:index]
before_filter :force_order_for_user,:except => [:index] before_filter :force_order_for_user, except: [:index]
before_filter :for_app_sub_manager,:except => [:index] before_filter :for_app_sub_manager, except: [:index]
before_filter :set_module_app
def index def index
@tags = get_tags @tags = get_tags
@module_apps = ModuleApp.where(has_tag: true)
end end
def new def new
@ -17,7 +17,7 @@ class Admin::TagsController < OrbitBackendController
end end
def create def create
@tag = @module_app ? @module_app.tags.create(params[:tag]) : Tag.create(params[:tag]) @tag = @module_app.module_tags.create(params[:tag].merge(is_default: true))
end end
def update def update
@ -32,16 +32,51 @@ class Admin::TagsController < OrbitBackendController
format.js { render 'js/remove_element', :locals => {:id => "#{dom_id @tag}"} } format.js { render 'js/remove_element', :locals => {:id => "#{dom_id @tag}"} }
end end
end end
protected def add_to_default
tags = Tag.find(params[:ids])
def get_tags tag_leases = tags.map{|t| t.tag_lease}
@tags = @module_app.blank? ? Tag.all : @module_app.tags tags.each {|t| t.update_attribute(:is_default, true)}
tag_leases.each {|t| t.update_attribute(:module_app_ids, module_app_ids << @module_app.id)}
end end
def set_module_app def merge
@module_app = ModuleApp.find(params[:module_app_id]) if params[:module_app_id] tags = Tag.find(params[:ids])
@module_app_id = @module_app.id rescue nil taggings = tags.map{|t| t.taggings}
tag_leases = tags.map{|t| t.tag_lease}
name = params[:name]
module_app_ids = tag_leases.map{|t| t.module_app_ids}.flatten.uniq
if tags.detect{|t| t.is_default == true} || module_app_ids.count > 1
merge_default_tags(name, taggings, module_app_ids)
else
merge_tags(name, tags, module_app_ids[0])
end
taggings.each(&:destroy)
tag_leases.each(&:destroy)
end
protected
def create_default_tag(name, taggings, module_app_ids)
create_tag(@module_app, name, taggings, module_app_ids, true)
end
def create_tag(module_app, name, taggings, module_app_ids=[module_app], default=false)
new_tag = module_app.tags.create(name_translations: name, is_default: default)
taggings.each do |tagging|
tagging.tag = new_tag
tagging.save
end
tag_lease = new_tag.create_tag_lease(module_app_ids: (module_app_ids << module_app).uniq, is_default: default)
end
def merge_default_tags(name, taggings, module_app_ids)
create_default_tag(name, taggings, module_app_ids)
end
def merge_tags(name, taggings, module_app_id)
module_app = ModuleApp.find(module_app_id)
create_tag(module_app, name, taggings)
end end
end end

View File

@ -8,7 +8,7 @@ class ModuleApp
field :title field :title
field :sidebar_order,type: Integer,default: 0 field :sidebar_order,type: Integer,default: 0
has_many :tags, as: :module_tag, dependent: :destroy has_and_belongs_to_many :module_tags, dependent: :destroy
def refetch_setting!(reg) def refetch_setting!(reg)
# %w{module_label category base_url version organization author intro update_info create_date}.each do |field| # %w{module_label category base_url version organization author intro update_info create_date}.each do |field|
@ -19,6 +19,7 @@ class ModuleApp
self[:get_widget_style] = reg.get_widgets self[:get_widget_style] = reg.get_widgets
self[:using_default_widget] = !reg.get_default_widget.blank? self[:using_default_widget] = !reg.get_default_widget.blank?
self[:widgets] = reg.get_widgets self[:widgets] = reg.get_widgets
self[:has_tag] = reg.get_has_tags
end end
@ -136,4 +137,8 @@ class ModuleApp
self.where(key: key)[0] rescue nil self.where(key: key)[0] rescue nil
end end
def tags
self.module_tags.map{|t| t.tag }
end
end end

21
app/models/module_tag.rb Normal file
View File

@ -0,0 +1,21 @@
class ModuleTag
include Mongoid::Document
include Mongoid::Timestamps
field :is_default, type: Boolean, deault: false
has_one :tag, as: :tag_lease, dependent: :destroy, autosave: true
has_and_belongs_to_many :module_apps
after_initialize :init_tag
private
def init_tag
if new_record?
self.build_tag(name_translations: self.name_translations, is_default: self.is_default)
self.remove_attribute(:name_translations)
end
end
end

View File

@ -11,14 +11,13 @@ class Tag
is_impressionable :counter_cache => { :column_name => :view_count } is_impressionable :counter_cache => { :column_name => :view_count }
field :name, localize: true field :name, localize: true
field :view_count, :type => Integer, :default => 0 field :cloud_view_count, type: Integer, default: 0
field :cloud_view_count, :type => Integer, :default => 0 field :is_default, type: Boolean, deault: false
field :view_count, type: Integer, default: 0
belongs_to :module_tag, polymorphic: true belongs_to :tag_lease, polymorphic: true, dependent: :destroy
has_many :taggings, dependent: :destroy has_many :taggings, dependent: :destroy
#field :cloud_amper,:type: Integer,:default=> 0
def self.sorted_for_cloud def self.sorted_for_cloud
tags = {} tags = {}
self.all.each{ |tag| self.all.each{ |tag|

View File

@ -111,6 +111,8 @@ Orbit::Application.routes.draw do
end end
end end
resources :module_tags
resources :page_parts do resources :page_parts do
member do member do
get 'reload_after_widget_field_changed',:action=>'reload_widget_field' get 'reload_after_widget_field_changed',:action=>'reload_widget_field'

View File

@ -28,7 +28,7 @@ module OrbitApp
end end
class DataSheet class DataSheet
attr_reader :name,:key,:base_path,:module_label,:data_count attr_reader :name,:key,:base_path,:module_label,:data_count, :has_tag
def initialize(name, &block) def initialize(name, &block)
@name = name @name = name
@ -37,6 +37,7 @@ module OrbitApp
@front_end_app_pages = nil @front_end_app_pages = nil
@module_label = 'rulingcom.errors.init.module_app_noname' @module_label = 'rulingcom.errors.init.module_app_noname'
@data_count = 1..15 # as default @data_count = 1..15 # as default
@has_tag = nil
block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given?
setup_module_app setup_module_app
end end
@ -148,6 +149,14 @@ module OrbitApp
define_method(field){|var| instance_variable_set( "@" + field, var)} define_method(field){|var| instance_variable_set( "@" + field, var)}
end end
def taggable
@has_tag = true
end
def get_has_tags
@has_tag.nil? ? false : true
end
end # of class DataSheet end # of class DataSheet
end end

View File

@ -1,3 +1 @@
# require "action_view"
require "orbit_tag/taggable" require "orbit_tag/taggable"

View File

@ -69,9 +69,12 @@ module OrbitTag
end end
module Tagging module Tagging
extend OrbitCoreLib::AppBackendUtility
def get_tags def get_tags
@module_app ? @module_app.tags : Tag.all @module_app.tags
end
def get_default_tags
Tag.where(default: true)
end end
end end
end end

View File

@ -21,6 +21,8 @@ module Announcement
category ["BulletinCategory"] category ["BulletinCategory"]
data_count 3..10 data_count 3..10
taggable
widgets do widgets do
default_widget do default_widget do
enable ["typeA","typeC"] enable ["typeA","typeC"]
@ -70,7 +72,7 @@ module Announcement
context_link 'tags', context_link 'tags',
:link_path=>"admin_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Announcement'}))" , :link_path=>"admin_module_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Announcement'}))" ,
:priority=>4, :priority=>4,
# :active_for_action=>{:bulletin_categorys=>:index}, # :active_for_action=>{:bulletin_categorys=>:index},
:available_for => [:manager] :available_for => [:manager]

View File

@ -19,6 +19,8 @@ module Archive
category ["ArchiveFileCategory"] category ["ArchiveFileCategory"]
taggable
widgets do widgets do
# default_widget do # default_widget do
# query 'ArchiveFile.all' # query 'ArchiveFile.all'
@ -66,7 +68,7 @@ module Archive
:available_for => [:manager] :available_for => [:manager]
context_link 'tags', context_link 'tags',
:link_path=>"admin_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Archive'}))" , :link_path=>"admin_module_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Archive'}))" ,
:priority=>4, :priority=>4,
# :active_for_action=>{:bulletin_categorys=>:index}, # :active_for_action=>{:bulletin_categorys=>:index},
:available_for => [:admin] :available_for => [:admin]

View File

@ -9,6 +9,8 @@ module Calendar
author "RD dep" author "RD dep"
intro "I am intro" intro "I am intro"
update_info 'some update_info' update_info 'some update_info'
taggable
side_bar do side_bar do
head_label_i18n 'calendar.calendar',:icon_class=>"icons-calendar" head_label_i18n 'calendar.calendar',:icon_class=>"icons-calendar"
@ -25,7 +27,7 @@ module Calendar
:available_for => [:manager] :available_for => [:manager]
context_link 'tags', context_link 'tags',
:link_path=>"admin_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Calendar'}))" , :link_path=>"admin_module_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Calendar'}))" ,
:priority=>4, :priority=>4,
# :active_for_action=>{:bulletin_categorys=>:index}, # :active_for_action=>{:bulletin_categorys=>:index},
:available_for => [:manager] :available_for => [:manager]

View File

@ -37,6 +37,8 @@ module Faq
# end # end
end end
taggable
side_bar do side_bar do
head_label_i18n 'faq.faq',:icon_class=>"icons-help" head_label_i18n 'faq.faq',:icon_class=>"icons-help"
available_for [:admin,:guest,:manager,:sub_manager] available_for [:admin,:guest,:manager,:sub_manager]
@ -64,7 +66,7 @@ module Faq
:available_for => [:manager] :available_for => [:manager]
context_link 'tags', context_link 'tags',
:link_path=>"admin_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Faq'}))" , :link_path=>"admin_module_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Faq'}))" ,
:priority=>4, :priority=>4,
# :active_for_action=>{:bulletin_categorys=>:index}, # :active_for_action=>{:bulletin_categorys=>:index},
:available_for => [:manager] :available_for => [:manager]

View File

@ -18,6 +18,8 @@ module Gallery
category ["gallery_categories"] category ["gallery_categories"]
taggable
widgets do widgets do
# default_widget do # default_widget do
# query 'Bulletin.all' # query 'Bulletin.all'
@ -55,7 +57,7 @@ module Gallery
:available_for => [:manager] :available_for => [:manager]
context_link 'tags', context_link 'tags',
:link_path=>"admin_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Gallery'}))" , :link_path=>"admin_module_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'Gallery'}))" ,
:priority=>4, :priority=>4,
# :active_for_action=>{:bulletin_categorys=>:index}, # :active_for_action=>{:bulletin_categorys=>:index},
:available_for => [:manager] :available_for => [:manager]

View File

@ -0,0 +1,4 @@
en:
module_name:
tag: Tag

View File

@ -0,0 +1,4 @@
zh_tw:
module_name:
tag: 標籤

33
vendor/built_in_modules/tag/init.rb vendored Normal file
View File

@ -0,0 +1,33 @@
module Tag
OrbitApp.registration "Tag", type: 'ModuleApp' do
module_label 'module_name.tag'
base_url File.expand_path File.dirname(__FILE__)
# version "0.1"
# organization "Rulingcom"
# author "RD dep"
# intro "I am intro"
# update_info 'some update_info'
side_bar do
head_label_i18n 'module_name.tag', icon_class: "icons-tag"
available_for [:admin, :manager]
active_for_controllers ({public: ['admin/tags']})
head_link_path "admin_tags_path"
context_link 'all',
link_path: "admin_tags_path",
priority: 1,
active_for_action: {tags: :index},
available_for: [:admin, :manager]
context_link 'module_authorization',
link_path: "admin_module_app_manager_auth_proc_path(ModuleApp.first(conditions: {title: 'Tag'}))",
priority: 2,
active_for_app_auth: 'tags',
available_for: [:admin]
end
end
end

View File

@ -12,6 +12,8 @@ module WebResource
category ["WebLinkCategory"] category ["WebLinkCategory"]
taggable
widgets do widgets do
# default_widget do # default_widget do
# query 'Bulletin.all' # query 'Bulletin.all'
@ -53,7 +55,7 @@ module WebResource
:available_for => [:manager] :available_for => [:manager]
context_link 'tags', context_link 'tags',
:link_path=>"admin_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'WebResource'}))" , :link_path=>"admin_module_tags_path(:module_app_id => ModuleApp.first(conditions: {title: 'WebResource'}))" ,
:priority=>4, :priority=>4,
# :active_for_action=>{:bulletin_categorys=>:index}, # :active_for_action=>{:bulletin_categorys=>:index},
:available_for => [:manager] :available_for => [:manager]