diff --git a/app/controllers/admin/module_tags_controller.rb b/app/controllers/admin/module_tags_controller.rb new file mode 100644 index 000000000..1eb3e801d --- /dev/null +++ b/app/controllers/admin/module_tags_controller.rb @@ -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 diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index a94f62d65..921cf3c7d 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -1,11 +1,11 @@ class Admin::TagsController < 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] - before_filter :set_module_app + 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 + @module_apps = ModuleApp.where(has_tag: true) end def new @@ -17,7 +17,7 @@ class Admin::TagsController < OrbitBackendController end 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 def update @@ -32,16 +32,51 @@ class Admin::TagsController < OrbitBackendController format.js { render 'js/remove_element', :locals => {:id => "#{dom_id @tag}"} } end end - - protected - - def get_tags - @tags = @module_app.blank? ? Tag.all : @module_app.tags + + def add_to_default + tags = Tag.find(params[:ids]) + tag_leases = tags.map{|t| t.tag_lease} + 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 - def set_module_app - @module_app = ModuleApp.find(params[:module_app_id]) if params[:module_app_id] - @module_app_id = @module_app.id rescue nil + def merge + tags = Tag.find(params[:ids]) + 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 diff --git a/app/models/module_app.rb b/app/models/module_app.rb index 649e135d9..d8fbae359 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -8,7 +8,7 @@ class ModuleApp field :title 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) # %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[:using_default_widget] = !reg.get_default_widget.blank? self[:widgets] = reg.get_widgets + self[:has_tag] = reg.get_has_tags end @@ -136,4 +137,8 @@ class ModuleApp self.where(key: key)[0] rescue nil end + def tags + self.module_tags.map{|t| t.tag } + end + end diff --git a/app/models/module_tag.rb b/app/models/module_tag.rb new file mode 100644 index 000000000..52eac9b43 --- /dev/null +++ b/app/models/module_tag.rb @@ -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 \ No newline at end of file diff --git a/app/models/tag.rb b/app/models/tag.rb index 30acb4c89..48c350c87 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -11,14 +11,13 @@ class Tag is_impressionable :counter_cache => { :column_name => :view_count } 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 - #field :cloud_amper,:type: Integer,:default=> 0 - def self.sorted_for_cloud tags = {} self.all.each{ |tag| diff --git a/app/views/admin/tags/_add.html.erb b/app/views/admin/module_tags/_add.html.erb similarity index 100% rename from app/views/admin/tags/_add.html.erb rename to app/views/admin/module_tags/_add.html.erb diff --git a/app/views/admin/tags/_form.html.erb b/app/views/admin/module_tags/_form.html.erb similarity index 100% rename from app/views/admin/tags/_form.html.erb rename to app/views/admin/module_tags/_form.html.erb diff --git a/app/views/admin/tags/_tag.html.erb b/app/views/admin/module_tags/_tag.html.erb similarity index 100% rename from app/views/admin/tags/_tag.html.erb rename to app/views/admin/module_tags/_tag.html.erb diff --git a/app/views/admin/tags/create.js.erb b/app/views/admin/module_tags/create.js.erb similarity index 100% rename from app/views/admin/tags/create.js.erb rename to app/views/admin/module_tags/create.js.erb diff --git a/app/views/admin/tags/edit.js.erb b/app/views/admin/module_tags/edit.js.erb similarity index 100% rename from app/views/admin/tags/edit.js.erb rename to app/views/admin/module_tags/edit.js.erb diff --git a/app/views/admin/tags/index.html.erb b/app/views/admin/module_tags/index.html.erb similarity index 100% rename from app/views/admin/tags/index.html.erb rename to app/views/admin/module_tags/index.html.erb diff --git a/app/views/admin/tags/update.js.erb b/app/views/admin/module_tags/update.js.erb similarity index 100% rename from app/views/admin/tags/update.js.erb rename to app/views/admin/module_tags/update.js.erb diff --git a/config/routes.rb b/config/routes.rb index 2e2dc8a55..4cd690201 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -111,6 +111,8 @@ Orbit::Application.routes.draw do end end + resources :module_tags + resources :page_parts do member do get 'reload_after_widget_field_changed',:action=>'reload_widget_field' diff --git a/lib/orbit_app/module/registration.rb b/lib/orbit_app/module/registration.rb index b81ae0356..45c3fc6f1 100644 --- a/lib/orbit_app/module/registration.rb +++ b/lib/orbit_app/module/registration.rb @@ -28,7 +28,7 @@ module OrbitApp end 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) @name = name @@ -37,6 +37,7 @@ module OrbitApp @front_end_app_pages = nil @module_label = 'rulingcom.errors.init.module_app_noname' @data_count = 1..15 # as default + @has_tag = nil block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? setup_module_app end @@ -148,6 +149,14 @@ module OrbitApp define_method(field){|var| instance_variable_set( "@" + field, var)} end + def taggable + @has_tag = true + end + + def get_has_tags + @has_tag.nil? ? false : true + end + end # of class DataSheet end diff --git a/lib/orbit_tag.rb b/lib/orbit_tag.rb index 055524928..61912534c 100644 --- a/lib/orbit_tag.rb +++ b/lib/orbit_tag.rb @@ -1,3 +1 @@ -# require "action_view" - require "orbit_tag/taggable" diff --git a/lib/orbit_tag/taggable.rb b/lib/orbit_tag/taggable.rb index 18fba1bdb..248145f6e 100644 --- a/lib/orbit_tag/taggable.rb +++ b/lib/orbit_tag/taggable.rb @@ -69,9 +69,12 @@ module OrbitTag end module Tagging - extend OrbitCoreLib::AppBackendUtility 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 diff --git a/vendor/built_in_modules/announcement/init.rb b/vendor/built_in_modules/announcement/init.rb index 72977f750..bbcd33429 100644 --- a/vendor/built_in_modules/announcement/init.rb +++ b/vendor/built_in_modules/announcement/init.rb @@ -21,6 +21,8 @@ module Announcement category ["BulletinCategory"] data_count 3..10 + taggable + widgets do default_widget do enable ["typeA","typeC"] @@ -70,7 +72,7 @@ module Announcement 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, # :active_for_action=>{:bulletin_categorys=>:index}, :available_for => [:manager] diff --git a/vendor/built_in_modules/archive/init.rb b/vendor/built_in_modules/archive/init.rb index c12fbbd31..acf9ae6ad 100644 --- a/vendor/built_in_modules/archive/init.rb +++ b/vendor/built_in_modules/archive/init.rb @@ -19,6 +19,8 @@ module Archive category ["ArchiveFileCategory"] + taggable + widgets do # default_widget do # query 'ArchiveFile.all' @@ -66,7 +68,7 @@ module Archive :available_for => [:manager] 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, # :active_for_action=>{:bulletin_categorys=>:index}, :available_for => [:admin] diff --git a/vendor/built_in_modules/calendar/init.rb b/vendor/built_in_modules/calendar/init.rb index ea2070c65..a1fded82c 100644 --- a/vendor/built_in_modules/calendar/init.rb +++ b/vendor/built_in_modules/calendar/init.rb @@ -9,6 +9,8 @@ module Calendar author "RD dep" intro "I am intro" update_info 'some update_info' + + taggable side_bar do head_label_i18n 'calendar.calendar',:icon_class=>"icons-calendar" @@ -25,7 +27,7 @@ module Calendar :available_for => [:manager] 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, # :active_for_action=>{:bulletin_categorys=>:index}, :available_for => [:manager] diff --git a/vendor/built_in_modules/faq/init.rb b/vendor/built_in_modules/faq/init.rb index f2d514461..1b05a37f9 100644 --- a/vendor/built_in_modules/faq/init.rb +++ b/vendor/built_in_modules/faq/init.rb @@ -37,6 +37,8 @@ module Faq # end end + taggable + side_bar do head_label_i18n 'faq.faq',:icon_class=>"icons-help" available_for [:admin,:guest,:manager,:sub_manager] @@ -64,7 +66,7 @@ module Faq :available_for => [:manager] 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, # :active_for_action=>{:bulletin_categorys=>:index}, :available_for => [:manager] diff --git a/vendor/built_in_modules/gallery/init.rb b/vendor/built_in_modules/gallery/init.rb index b13665853..9414adecb 100644 --- a/vendor/built_in_modules/gallery/init.rb +++ b/vendor/built_in_modules/gallery/init.rb @@ -18,6 +18,8 @@ module Gallery category ["gallery_categories"] + taggable + widgets do # default_widget do # query 'Bulletin.all' @@ -55,7 +57,7 @@ module Gallery :available_for => [:manager] 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, # :active_for_action=>{:bulletin_categorys=>:index}, :available_for => [:manager] diff --git a/vendor/built_in_modules/tag/config/locales/en.yml b/vendor/built_in_modules/tag/config/locales/en.yml new file mode 100644 index 000000000..cf2dc003b --- /dev/null +++ b/vendor/built_in_modules/tag/config/locales/en.yml @@ -0,0 +1,4 @@ +en: + + module_name: + tag: Tag \ No newline at end of file diff --git a/vendor/built_in_modules/tag/config/locales/zh_tw.yml b/vendor/built_in_modules/tag/config/locales/zh_tw.yml new file mode 100644 index 000000000..5857e0b74 --- /dev/null +++ b/vendor/built_in_modules/tag/config/locales/zh_tw.yml @@ -0,0 +1,4 @@ +zh_tw: + + module_name: + tag: 標籤 \ No newline at end of file diff --git a/vendor/built_in_modules/tag/init.rb b/vendor/built_in_modules/tag/init.rb new file mode 100644 index 000000000..a59fb3b6b --- /dev/null +++ b/vendor/built_in_modules/tag/init.rb @@ -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 diff --git a/vendor/built_in_modules/web_resource/init.rb b/vendor/built_in_modules/web_resource/init.rb index 2ba99e7a0..b7e8bc1e0 100644 --- a/vendor/built_in_modules/web_resource/init.rb +++ b/vendor/built_in_modules/web_resource/init.rb @@ -12,6 +12,8 @@ module WebResource category ["WebLinkCategory"] + taggable + widgets do # default_widget do # query 'Bulletin.all' @@ -53,7 +55,7 @@ module WebResource :available_for => [:manager] 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, # :active_for_action=>{:bulletin_categorys=>:index}, :available_for => [:manager]