From 7f325885518bdc648e99f7849313508300bb8de6 Mon Sep 17 00:00:00 2001 From: manson Date: Mon, 21 Apr 2014 10:48:36 +0800 Subject: [PATCH] Tag --- Gemfile | 1 + app/assets/javascripts/admin/tags.js | 21 +++ app/assets/javascripts/back_end.js | 7 +- app/assets/javascripts/lib/tags.js.erb | 4 +- app/controllers/admin/tags_controller.rb | 104 +++++++++++++ app/controllers/application_controller.rb | 10 +- app/controllers/sessions_controller.rb | 2 +- app/helpers/admin/tags_helper.rb | 3 + app/helpers/orbit_helper.rb | 112 +++++++++----- app/models/module_app.rb | 20 +++ app/models/tag.rb | 23 +++ app/models/tagging.rb | 7 + app/views/admin/tags/_delete_modal.html.erb | 16 ++ app/views/admin/tags/_form.html.erb | 9 ++ app/views/admin/tags/_index.html.erb | 42 +++++ app/views/admin/tags/_merge_modal.html.erb | 28 ++++ app/views/admin/tags/_tag.html.erb | 21 +++ app/views/admin/tags/index.html.erb | 35 +++++ app/views/admin/tags/index.js.erb | 9 ++ app/views/admin/tags/new.html.erb | 1 + app/views/layouts/back_end.html.erb | 22 ++- app/views/sessions/new.html.erb | 86 +++++------ config/environment.rb | 17 +++ config/initializers/impression.rb | 6 + config/initializers/orbit_tag.rb | 19 +++ config/locales/en.yml | 13 ++ config/routes.rb | 11 +- lib/orbit_app/module/registration.rb | 161 +++++++++++--------- lib/orbit_tag/taggable.rb | 22 +++ test/fixtures/taggings.yml | 11 ++ test/fixtures/tags.yml | 11 ++ test/models/tag_test.rb | 7 + test/models/tagging_test.rb | 7 + 33 files changed, 704 insertions(+), 164 deletions(-) create mode 100644 app/assets/javascripts/admin/tags.js create mode 100644 app/controllers/admin/tags_controller.rb create mode 100644 app/helpers/admin/tags_helper.rb create mode 100644 app/models/tag.rb create mode 100644 app/models/tagging.rb create mode 100644 app/views/admin/tags/_delete_modal.html.erb create mode 100644 app/views/admin/tags/_form.html.erb create mode 100644 app/views/admin/tags/_index.html.erb create mode 100644 app/views/admin/tags/_merge_modal.html.erb create mode 100644 app/views/admin/tags/_tag.html.erb create mode 100644 app/views/admin/tags/index.html.erb create mode 100644 app/views/admin/tags/index.js.erb create mode 100644 app/views/admin/tags/new.html.erb create mode 100644 config/initializers/impression.rb create mode 100644 config/initializers/orbit_tag.rb create mode 100644 lib/orbit_tag/taggable.rb create mode 100644 test/fixtures/taggings.yml create mode 100644 test/fixtures/tags.yml create mode 100644 test/models/tag_test.rb create mode 100644 test/models/tagging_test.rb diff --git a/Gemfile b/Gemfile index 19c41e5..6743862 100644 --- a/Gemfile +++ b/Gemfile @@ -31,6 +31,7 @@ gem "mini_magick", github: 'minimagick/minimagick' gem 'carrierwave' gem 'carrierwave-mongoid', :require => 'carrierwave/mongoid' gem 'mongoid-grid_fs', github: 'ahoward/mongoid-grid_fs' +gem "impressionist" #built in modules eval(File.read(File.dirname(__FILE__) + '/built_in_extensions.rb')) diff --git a/app/assets/javascripts/admin/tags.js b/app/assets/javascripts/admin/tags.js new file mode 100644 index 0000000..3283b23 --- /dev/null +++ b/app/assets/javascripts/admin/tags.js @@ -0,0 +1,21 @@ +//= require lib/tags + +$(function() { + $.pageslide.closeCallback(function(pageslide, item) { + $('.filter-item').removeClass("active"); + }); + $.pageslide.loadComplete(function(pageslide, item) { + $('.filter-item').removeClass("active"); + item.closest('li').addClass('active'); + if(item.data('id') == 'new') { + resetForm(); + pageslide.find('form').attr('action', '/admin/tags/'); + pageslide.find('form').attr('method', 'post'); + } + else { + setForm(item.data('form')); + pageslide.find('form').attr('action', '/admin/tags/' + item.data('id')); + pageslide.find('form').attr('method', 'put'); + } + }); +}) \ No newline at end of file diff --git a/app/assets/javascripts/back_end.js b/app/assets/javascripts/back_end.js index 9bdcf86..0b2af80 100644 --- a/app/assets/javascripts/back_end.js +++ b/app/assets/javascripts/back_end.js @@ -1 +1,6 @@ -//= require basic \ No newline at end of file +//= require basic +//= require lib/footable-0.1.js +//= require lib/all-list +//= require lib/jquery.fastLiveFilter.js +//= require lib/checkbox.card.js +//= require lib/jquery.form.js \ No newline at end of file diff --git a/app/assets/javascripts/lib/tags.js.erb b/app/assets/javascripts/lib/tags.js.erb index 92593a5..b24d0d2 100755 --- a/app/assets/javascripts/lib/tags.js.erb +++ b/app/assets/javascripts/lib/tags.js.erb @@ -71,14 +71,14 @@ function checkedLength() { $defaultTags.each(function(i) { ids.push($defaultTags.eq(i).parent().siblings('.tag_id').val()); }); - $('#removeDefaults').attr('rel', "<%= Rails.application.routes.url_helpers.remove_default_admin_module_tags_path %>" + "?module_app_id=" + $('#module_app_id').val() + "&ids=" + ids); + $('#removeDefaults').attr('rel', "<%= Rails.application.routes.url_helpers.remove_default_admin_tags_path %>" + "?module_app_id=" + $('#module_app_id').val() + "&ids=" + ids); $('#removeDefaults').removeClass('hide'); } else { var ids = new Array(); $moduleTags.each(function(i) { ids.push($moduleTags.eq(i).parent().siblings('.tag_id').val()); }); - $('#deleteTags').attr('rel', "<%= Rails.application.routes.url_helpers.delete_tags_admin_module_tags_path %>" + "?module_app_id=" + $('#module_app_id').val() + "&ids=" + ids); + $('#deleteTags').attr('rel', "<%= Rails.application.routes.url_helpers.delete_tags_admin_tags_path %>" + "?module_app_id=" + $('#module_app_id').val() + "&ids=" + ids); $('#deleteTags').removeClass('hide'); if($moduleTags.length > 1) { $('#mergerTags').removeClass('hide'); diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb new file mode 100644 index 0000000..513c250 --- /dev/null +++ b/app/controllers/admin/tags_controller.rb @@ -0,0 +1,104 @@ +class Admin::TagsController < ApplicationController + before_filter :setup_vars + + layout "back_end" + + def index + @tag = Tag.new + @tags = Tag.where(:is_default=>true) + @module_apps = ModuleApp.where(taggable: true) + end + + def new + @tag = Tag.new + render layout: false + end + + def edit + @tag = Tag.find(params[:id]) + render layout: false + end + + def create + @tag = Tag.new(tag_params.merge(is_default: true)) + if @tag.save + redirect_to admin_tags_url + else + @tag = Tag.new(tag_params) + flash.now[:error] = t('create.error.tag') + render :action => "new" + end + end + + def update + @tag = Tag.find(params[:id]) + if @tag.update_attributes(tag_params) + redirect_to action: :index, :status => 303 + else + flash.now[:error] = t('update.error.tag') + render :action => "edit" + end + end + + def delete_tags + tags = Tag.find(params[:ids].split(',')) rescue nil + if tags + tags.each(&:destroy) + end + redirect_to admin_tags_url + end + + def add_to_default + tags = Tag.find(params[:ids].split(',')) rescue nil + if tags + tags.each do |tag| + tag.update_attribute(:is_default, true) + end + end + redirect_to admin_tags_url + end + + def merge + tags = Tag.find(params[:ids]) + new_tag = Tag.new tag_params + + mods = [] + + tags.each do |tag| + new_tag.is_default = true if tag.is_default + + tag.taggings.each do |tagging| + taggable = tagging.taggable + taggable.tags = taggable.tags - [tag] + [new_tag] + taggable.save + end + + unless tag.module_app.nil? + tag.module_app.each do |mod| + mods << mod + mod.tags = mod.tags - [tag] + [new_tag] + mod.save + end + end + + tag.destroy + end + + new_tag.is_default = true if mods.flatten.uniq.size > 1 + + new_tag.save + + redirect_to admin_tags_url + end + + def tag_params + params.require(:tag).permit! + end + + protected + + def setup_vars + @module_app = ModuleApp.where(:key=>"tag").first + end + +end \ No newline at end of file diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index ad71e4e..c6f2b26 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -22,13 +22,13 @@ class ApplicationController < ActionController::Base end def current_site - @current_site = Site.find_by(site_active: true) + @current_site = Site.first end private def current_user - @current_user ||= User.find(session[:user_id]) if session[:user_id] + @current_user ||= User.find(session[:user_id]) if session[:user_id] rescue nil end protected @@ -36,8 +36,10 @@ class ApplicationController < ActionController::Base def authenticate_user if session[:user_id] # set current user object to @current_user object variable - @current_user = User.find(session[:user_id]) - return true + @current_user = User.find(session[:user_id]) rescue nil + + redirect_to new_session_path if @current_user.nil? + return true else redirect_to new_session_path return false diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 2bf91f2..c3ffda6 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -5,7 +5,7 @@ class SessionsController < ApplicationController end def create - user = User.find_by(user_name: params[:user_name]) + user = User.find_by(user_name: params[:user_name]) rescue nil if user && user.authenticate(params[:password]) session[:user_id] = user.id redirect_to admin_dashboards_path, :notice => "Logged in!" diff --git a/app/helpers/admin/tags_helper.rb b/app/helpers/admin/tags_helper.rb new file mode 100644 index 0000000..5481c99 --- /dev/null +++ b/app/helpers/admin/tags_helper.rb @@ -0,0 +1,3 @@ +module Admin::TagsHelper + +end \ No newline at end of file diff --git a/app/helpers/orbit_helper.rb b/app/helpers/orbit_helper.rb index 68fe445..bb0a33d 100644 --- a/app/helpers/orbit_helper.rb +++ b/app/helpers/orbit_helper.rb @@ -1,47 +1,85 @@ module OrbitHelper - def self.set_params(params) - @params = params - end + def self.set_params(params) + @params = params + end - def self.get_params - @params - end + def self.get_params + @params + end - def self.url_to_show(slug) - if @params[:url] - "/#{@site_locale}#{@params[:url]}/#{slug}" - else - page = Page.where(:module => self.current_widget_module).first - "/#{@site_locale}#{page.url}/#{slug}" - end - end + def self.url_to_show(slug) + if @params[:url] + "/#{@site_locale}#{@params[:url]}/#{slug}" + else + page = Page.where(:module => self.current_widget_module).first + "/#{@site_locale}#{page.url}/#{slug}" + end + end - def page_url(url) - "#{request.host_with_port}/#{locale}#{url}" - end + def page_url(url) + "#{request.host_with_port}/#{locale}#{url}" + end - def self.set_site_locale(locale) - @site_locale = locale - end + def self.set_site_locale(locale) + @site_locale = locale + end - def self.get_site_locale - @site_locale - end + def self.get_site_locale + @site_locale + end - def self.current_widget_module - @controller_name - end + def self.current_widget_module + @controller_name + end - def self.set_current_widget_module(name) - @controller_name = name - end + def self.set_current_widget_module(name) + @controller_name = name + end + + def get_item_module_infos(page) + if page.parent_page_id.nil? + ["Home","icons-house"] + else + module_app = ModuleApp.where(:key => page.module).first + [module_app.title, module_app.get_registration.icon_class] + end + end + + # =============================================================== + # Breadcrumbs + # =============================================================== + def back_end_breadcrumb + res = '' + divider = "/" + res << "
  • #{t(:dashboard_)}#{divider}
  • " + case controller.controller_name + when 'authorizations' + res << "
  • #{@module_app.title}#{divider}
  • " + case params[:type] + when 'approval' + res << "
  • #{t(:approval_)}
  • " + when 'category' + res << "
  • #{t(:category_auth)}
  • " + when nil + res << "
  • #{t(:module_authorization)}
  • " + else + res << "
  • #{params[:type].underscore.humanize.capitalize} #{t(:authorization_)}
  • " + end + when 'categories' + res << "
  • #{@module_app.title}#{divider}
  • " + res << "
  • #{t(:categories)}
  • " + when 'module_tags' + res << "
  • #{@module_app.title}#{divider}
  • " + res << "
  • #{t(:tags)}
  • " + else + if params[:action].eql?('index') + res << "
  • #{@module_app.title}
  • " + else + res << "
  • #{@module_app.title}#{divider}
  • " + res << "
  • #{t(params[:action], scope: 'restful_actions')}
  • " + end + end + res.html_safe + end - def get_item_module_infos(page) - if page.parent_page_id.nil? - ["Home","icons-house"] - else - module_app = ModuleApp.where(:key => page.module).first - [module_app.title, module_app.get_registration.icon_class] - end - end end diff --git a/app/models/module_app.rb b/app/models/module_app.rb index 5bcb83e..45c85e4 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -4,9 +4,17 @@ class ModuleApp field :title, type: String field :key, type: String + field :taggable, type: Boolean, default: false + field :categorizable, type: Boolean, default: false field :sidebar_order,type: Integer,default: 0 has_many :categories, dependent: :destroy, :autosave => true + has_and_belongs_to_many :tags, dependent: :destroy, :autosave => true + + def refetch_setting!(reg) + self[:taggable] = reg.is_taggable + self[:categorizable] = reg.is_categorizable + end def get_registration OrbitApp::Module::Registration.find_by_key(key) @@ -28,4 +36,16 @@ class ModuleApp reg = get_registration reg.nil? ? 'Init is not defined completely' : get_registration.get_label_i18n end + + def taggable + get_registration.is_taggable + end + + def taggable_model + get_registration.taggable_model + end + + def categorizable + get_registration.is_categorizable + end end diff --git a/app/models/tag.rb b/app/models/tag.rb new file mode 100644 index 0000000..cc19bcf --- /dev/null +++ b/app/models/tag.rb @@ -0,0 +1,23 @@ +class Tag + include Mongoid::Document + include Mongoid::Timestamps + + field :name, localize: true + field :is_default, type: Boolean, default: false + + has_many :taggings, dependent: :destroy + has_and_belongs_to_many :module_app + + def show_names_slash + span_names = [] + Site.first.in_use_locales.each do |locale| + span_names << "#{self.name_translations[locale]}" + end + span_names.join(" / ").html_safe + end + + def get_module_tagging_count(model) + Tagging.where(:tag=>self.id, :taggable_type=>model).count + end + +end diff --git a/app/models/tagging.rb b/app/models/tagging.rb new file mode 100644 index 0000000..70544f3 --- /dev/null +++ b/app/models/tagging.rb @@ -0,0 +1,7 @@ +class Tagging + include Mongoid::Document + include Mongoid::Timestamps + + belongs_to :tag + belongs_to :taggable, polymorphic: true +end \ No newline at end of file diff --git a/app/views/admin/tags/_delete_modal.html.erb b/app/views/admin/tags/_delete_modal.html.erb new file mode 100644 index 0000000..a2623e3 --- /dev/null +++ b/app/views/admin/tags/_delete_modal.html.erb @@ -0,0 +1,16 @@ + \ No newline at end of file diff --git a/app/views/admin/tags/_form.html.erb b/app/views/admin/tags/_form.html.erb new file mode 100644 index 0000000..cd42406 --- /dev/null +++ b/app/views/admin/tags/_form.html.erb @@ -0,0 +1,9 @@ +<%#= flash_messages %> +<%#= f.error_messages %> + +<%= f.fields_for :name_translations do |f| %> + <% Site.first.in_use_locales.each do |locale| %> + <%= f.label :locale, "#{t(:name)} #{t(locale)}" %> + <%= f.text_field locale, class: 'input-large', placeholder: "#{t(:name)} #{t(locale)}", value: (@tag.name_translations[locale] rescue nil), id: locale %> + <% end %> +<% end %> diff --git a/app/views/admin/tags/_index.html.erb b/app/views/admin/tags/_index.html.erb new file mode 100644 index 0000000..8f73503 --- /dev/null +++ b/app/views/admin/tags/_index.html.erb @@ -0,0 +1,42 @@ + +
    +
    + + + <%= link_to t(:delete), '#', id: "deleteTags", class: "btn btn-danger toggable hide", rel: '' %> + <%= link_to t(:merge), '#', id: "mergerTags", class: "btn btn-success toggable hide", rel: merge_admin_tags_path %> + <%= link_to t(:add_to_default), add_to_default_admin_tags_path, id: "addDefault", class: "btn btn-info toggable hide", method: :post, remote: true %> + <%= link_to content_tag(:i, nil, class: "icons-plus") + " " + t(:add), '#', class: "btn btn-primary open-slide", data: {title: t('new.tag'), id: 'new'} %> +
    +
    + + + +
    +
    +

    <%= t(:default) %> <%= @tags.count %>

    +
      + <%= render partial: "tag", collection: @tags %> +
    + <% @module_apps.each do |mod| %> + <% + @mod = mod + tags = mod.tags + %> + <% icon_name = OrbitApp::Module::Registration.find_by_key(mod.key).get_icon_class rescue 'icons-daniel-bruce-2' %> +

    <%= mod.title %> <%= tags.count %>

    +
      + <%= render partial: "tag", collection: tags, locals: {in_module: true} %> +
    + <% end %> +
    +
    + + + +<%= render 'merge_modal' %> + + + +<%= render 'delete_modal' %> + \ No newline at end of file diff --git a/app/views/admin/tags/_merge_modal.html.erb b/app/views/admin/tags/_merge_modal.html.erb new file mode 100644 index 0000000..5f0c098 --- /dev/null +++ b/app/views/admin/tags/_merge_modal.html.erb @@ -0,0 +1,28 @@ + \ No newline at end of file diff --git a/app/views/admin/tags/_tag.html.erb b/app/views/admin/tags/_tag.html.erb new file mode 100644 index 0000000..6d050fc --- /dev/null +++ b/app/views/admin/tags/_tag.html.erb @@ -0,0 +1,21 @@ +
  • + + <% unless defined?(in_module) && tag.is_default %> +

    + +

    + <%= hidden_field_tag "ids[]", tag.id, class: "tag_id" %> + <% end %> + + <% if defined?(in_module) && tag.is_default %> + + <%= tag.get_module_tagging_count @mod.taggable_model %> + <%= tag.show_names_slash %> + + <% else %> + <%= link_to '#', class: "open-slide", data: {title: t('editing.tag'), id: tag.id.to_s, form: tag.name_translations} do %> + <%= tag.taggings.count %> + <%= tag.show_names_slash %> + <% end %> + <% end %> +
  • \ No newline at end of file diff --git a/app/views/admin/tags/index.html.erb b/app/views/admin/tags/index.html.erb new file mode 100644 index 0000000..6d031fa --- /dev/null +++ b/app/views/admin/tags/index.html.erb @@ -0,0 +1,35 @@ +<% content_for :right_nav do %> +
    + +
    +<% end %> + +
    + <%= render 'index' %> +
    + + +
    +
    + + + + +
    +
    +
    +
    + <%= form_for :tag, url: nil, remote: true do |f| %> +
    + <%= render :partial => "form", :locals => { :f => f } %> +
    + <%= t(:cancel) %> + <%= f.submit t(:submit), class: 'btn btn-primary btn-small' %> +
    +
    + <% end %> +
    +
    +
    +
    + \ No newline at end of file diff --git a/app/views/admin/tags/index.js.erb b/app/views/admin/tags/index.js.erb new file mode 100644 index 0000000..ac7e2ee --- /dev/null +++ b/app/views/admin/tags/index.js.erb @@ -0,0 +1,9 @@ +$("#delete_tags").modal('hide'); +$("#tags-merger").modal('hide'); +$("#tags_index").html("<%= j render 'index' %>") +$.pageslide.close(); +openSlide(); +$('.card').cardCheck({ + item: $('.card input[type="checkbox"]'), +}); +checkedLength() \ No newline at end of file diff --git a/app/views/admin/tags/new.html.erb b/app/views/admin/tags/new.html.erb new file mode 100644 index 0000000..41230ac --- /dev/null +++ b/app/views/admin/tags/new.html.erb @@ -0,0 +1 @@ +<%= render 'new' %> diff --git a/app/views/layouts/back_end.html.erb b/app/views/layouts/back_end.html.erb index 38bc8c8..2c59170 100644 --- a/app/views/layouts/back_end.html.erb +++ b/app/views/layouts/back_end.html.erb @@ -5,14 +5,32 @@ <%= render 'shared/meta' %> <%= render 'shared/google_font' %> <%= stylesheet_link_tag "back_end", media: "all", "data-turbolinks-track" => true %> + <%= stylesheet_link_tag params[:controller] if Rails.application.assets.find_asset "#{params[:controller]}.css" %> + <%= yield :page_specific_css %> <%= javascript_include_tag "back_end" %> <%= render 'shared/ie_html5_fix' %> + <%= javascript_include_tag params[:controller] if Rails.application.assets.find_asset "#{params[:controller]}.js" %> + <%= yield :page_specific_javascript %> <%= csrf_meta_tags %> - <%= render 'layouts/orbit_bar_backend' %> <%= render 'layouts/side_bar' %> - <%= yield %> +
    +
    +
    + + <%= yield :right_nav %> +
    + <%= yield %> +
    +
    + <%= javascript_include_tag "lib/pageslide.js" %> diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index 2cbaa19..d6e559b 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -1,51 +1,41 @@ -
    -
    \ No newline at end of file diff --git a/config/environment.rb b/config/environment.rb index 6b85183..fcb2a11 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -3,9 +3,26 @@ require File.expand_path('../application', __FILE__) # Initialize the Rails application. Orbit::Application.initialize! + +if Site.count == 0 + site = Site.new + site.title = "Orbit" + site.valid_locales = [:en, :zh_tw] + site.in_use_locales = site.valid_locales + site.save +end + if Page.count == 0 home = Page.new home.name_translations = {:en=>"home",:zh_tw=>"首頁"} home.url = "" home.save end + +if User.count==0 + user = User.new + user.user_name = "rulingcom" + user.password = "bjo4xjp6" + user.email = "orbit@rulingcom.com" + user.save +end diff --git a/config/initializers/impression.rb b/config/initializers/impression.rb new file mode 100644 index 0000000..7372872 --- /dev/null +++ b/config/initializers/impression.rb @@ -0,0 +1,6 @@ +# Use this hook to configure impressionist parameters +Impressionist.setup do |config| + # Define ORM. Could be :active_record (default), :mongo_mapper or :mongoid + # config.orm = :active_record + config.orm = :mongoid +end \ No newline at end of file diff --git a/config/initializers/orbit_tag.rb b/config/initializers/orbit_tag.rb new file mode 100644 index 0000000..ff13b84 --- /dev/null +++ b/config/initializers/orbit_tag.rb @@ -0,0 +1,19 @@ + OrbitApp.registration "Tag", type: 'ModuleApp' do + module_label 'module_name.tag' + base_url File.expand_path File.dirname(__FILE__) + + side_bar do + head_label_i18n 'module_name.tag', icon_class: "icons-tag" + head_link_path "admin_tags_path" + active_for_controllers ({public: ['admin/tags']}) + + # available_for [:admin, :manager] + + # context_link 'all', + # link_path: "admin_tags_path", + # priority: 1, + # active_for_action: {tags: :index}, + # available_for: [:admin, :manager] + + end + end diff --git a/config/locales/en.yml b/config/locales/en.yml index 154c41e..cf70dd3 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -20,16 +20,22 @@ # available at http://guides.rubyonrails.org/i18n.html. en: + en: English + zh_tw: Chinese _locale: English more: "More" + search: + tags: Search Tags site_: Site site_info: Site Info site_map: Site Map sitemap: Sitemap site_name: Site Name site_structure: Structure + submit: Submit modules: Modules mobile_settings: Mobile Settings + name: Name search_engine: Search Engine templates: Templates preference: Preferences @@ -44,6 +50,10 @@ en: password: Password all: All add: Add + new: + tag: New tag + editing: + tag: Editing tag site: system_preference: System Preference @@ -81,3 +91,6 @@ en: dots: ●●●●●● register: Register registered: Registered + + module_name: + tag: Tag diff --git a/config/routes.rb b/config/routes.rb index 1176661..56fb2f1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,7 +23,7 @@ Orbit::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 I18n.available_locales + locales = Site.first.in_use_locales rescue I18n.available_locales scope "(:locale)", locale: Regexp.new(locales.join("|")) do resources :users @@ -31,6 +31,15 @@ Orbit::Application.routes.draw do namespace :admin do resources :dashboards resources :items + resources :tags do + collection do + post 'add_to_default' + post 'delete_tags' + post 'merge' + post 'remove_default' + post 'update_included_default' + end + end resources :sites do get 'mail_setting' diff --git a/lib/orbit_app/module/registration.rb b/lib/orbit_app/module/registration.rb index 2cde63a..893f4d4 100644 --- a/lib/orbit_app/module/registration.rb +++ b/lib/orbit_app/module/registration.rb @@ -1,88 +1,113 @@ #This is a core class of Orbit Kernel. This class will register a module and set the properties defined in the module's initializer #file. module OrbitApp - module Module - module Registration - module ClassMethods - - @@registrations = [] + module Module + module Registration + module ClassMethods + + @@registrations = [] - def new(name,&block) - @@registrations << RegisteredModule.new(name,&block) - end + def new(name,&block) + @@registrations << RegisteredModule.new(name,&block) + end - def find_by_key(key) - @@registrations.each{|t| - return t if t.key.eql?(key) - } - return nil - end + def find_by_key(key) + @@registrations.each{|t| + return t if t.key.eql?(key) + } + return nil + end - def all - return @@registrations - end - end + def all + return @@registrations + end + end - extend ClassMethods - def self.included(other) - other.extend( ClassMethods ) - end + extend ClassMethods + def self.included(other) + other.extend( ClassMethods ) + end - class RegisteredModule - attr_reader :name,:key,:module_label,:widget_methods + class RegisteredModule + attr_reader :name,:key,:module_label,:widget_methods - def initialize(name,&block) - @name = name - @key = @name.underscore.singularize - @side_bar = nil - @module_label = @name - @widget_methods = [] - block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? - setup_module_app - end + def initialize(name,&block) + @name = name + @key = @name.underscore.singularize + @side_bar = nil + @module_label = @name + @widget_methods = [] + @is_taggable = false + @taggable_model = nil + @is_categorizable = false + block.arity < 1 ? instance_eval(&block) : block.call(self) if block_given? + setup_module_app + end - def get_module_app - ModuleApp.find_by(key: @key, title: name) rescue nil - end + def get_module_app + ModuleApp.find_by(key: @key, title: name) rescue nil + end - def setup_module_app - module_app = get_module_app - module_app = ModuleApp.new(:key=>@key,:title=>name) if module_app.nil? - module_app.save(:validate=>false) - end + def setup_module_app + module_app = get_module_app + module_app = ModuleApp.new(:key=>@key,:title=>name) if module_app.nil? + module_app.refetch_setting!(self) + module_app.save(:validate=>false) + end - %w{data_count module_label category base_url version organization author intro update_info}.each do |field| - define_method(field){|var| instance_variable_set( "@" + field, var)} - end + %w{data_count module_label category base_url version organization author intro update_info}.each do |field| + define_method(field){|var| instance_variable_set( "@" + field, var)} + end - def module_label(name = @name) - @module_label = name - end + def module_label(name = @name) + @module_label = name + end - def widget_methods(widgets) - @widget_methods = widgets - end + def widget_methods(widgets) + @widget_methods = widgets + end - def get_widget_methods - @widget_methods - end + def get_widget_methods + @widget_methods + end - def get_icon_class - @side_bar.get_icon_class - end + def get_icon_class + @side_bar.get_icon_class + end - def side_bar(&block) #setter for side_bar from init - @side_bar = SideBarRegistration::SideBar.new(@name,@key,method(:get_module_app), &block) - end - def get_side_bar - @side_bar - end + def side_bar(&block) #setter for side_bar from init + @side_bar = SideBarRegistration::SideBar.new(@name,@key,method(:get_module_app), &block) + end + def get_side_bar + @side_bar + end - def icon_class - @side_bar.get_icon_class - end + def icon_class + @side_bar.get_icon_class + end - end - end - end + def taggable(model) + @is_taggable = true + @taggable_model = model.classify + end + + def is_taggable + @is_taggable + end + + def taggable_model + @taggable_model + end + + def categorizable + @is_categorizable = true + end + + def is_categorizable + @is_categorizable + end + + end + end + end end \ No newline at end of file diff --git a/lib/orbit_tag/taggable.rb b/lib/orbit_tag/taggable.rb new file mode 100644 index 0000000..72085a0 --- /dev/null +++ b/lib/orbit_tag/taggable.rb @@ -0,0 +1,22 @@ +module OrbitTag + module Taggable + + def self.included(base) + base.has_many :taggings, as: :taggable, autosave: true, dependent: :destroy + base.accepts_nested_attributes_for :taggings, allow_destroy: true + end + + def tags + self.taggings.blank? ? [] : self.taggings.map{|t| t.tag}.compact + end + + def tags=(tags) + tags = [tags].flatten.uniq + Tagging.where(:taggable=>self.id).destroy_all + tags.each do |tag| + self.taggings.build(tag: tag) + end + end + + end +end \ No newline at end of file diff --git a/test/fixtures/taggings.yml b/test/fixtures/taggings.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/taggings.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/fixtures/tags.yml b/test/fixtures/tags.yml new file mode 100644 index 0000000..937a0c0 --- /dev/null +++ b/test/fixtures/tags.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the '{}' from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/tag_test.rb b/test/models/tag_test.rb new file mode 100644 index 0000000..b8498a1 --- /dev/null +++ b/test/models/tag_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TagTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end diff --git a/test/models/tagging_test.rb b/test/models/tagging_test.rb new file mode 100644 index 0000000..77dbeac --- /dev/null +++ b/test/models/tagging_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class TaggingTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end +end