From b315215bed45f23179402ee59bb8dbff3e59d895 Mon Sep 17 00:00:00 2001 From: Fu Matthew Date: Wed, 6 Feb 2013 19:51:53 +0800 Subject: [PATCH] first version of multi categories and tags --- .../javascripts/inc/update_cates_and_tags.js | 17 +++++ .../admin/page_parts_controller.rb | 12 +++ app/controllers/admin/pages_controller.rb | 8 ++ app/controllers/default_widget_controller.rb | 75 +++++++++++++------ app/helpers/admin/page_parts_helper.rb | 21 +++++- app/models/page.rb | 4 +- app/models/page_part.rb | 2 + .../_widget_data_source_category.html.erb | 4 +- .../_widget_data_source_tag.html.erb | 4 +- app/views/admin/page_parts/edit.html.erb | 8 +- app/views/admin/pages/edit.html.erb | 1 + lib/tasks/migrate.rake | 20 +++++ 12 files changed, 142 insertions(+), 34 deletions(-) create mode 100644 app/assets/javascripts/inc/update_cates_and_tags.js diff --git a/app/assets/javascripts/inc/update_cates_and_tags.js b/app/assets/javascripts/inc/update_cates_and_tags.js new file mode 100644 index 00000000..bb41a183 --- /dev/null +++ b/app/assets/javascripts/inc/update_cates_and_tags.js @@ -0,0 +1,17 @@ +function update_cates_and_tags() +{ + $('.select_option,.select_all').removeAttr('disabled'); + $(".select_all:checked").each(function( obj ) { + $(this).parent().siblings('label').find('.select_option').attr('disabled',true); + $(this).parent().siblings('label').find('.select_option').removeAttr('checked'); + }); + $(".select_option:checked").each(function( obj ) { + $(this).parent().siblings('label').find('.select_all').attr('disabled',true); + $(this).parent().siblings('label').find('.select_all').removeAttr('checked'); + }); +} + +$(document).ready(function(){ + update_cates_and_tags(); + $("#widget_data_source_category,#widget_data_source_tag,#app_page_category,#app_page_tag").find('input').change(function(){update_cates_and_tags()}); +}); \ No newline at end of file diff --git a/app/controllers/admin/page_parts_controller.rb b/app/controllers/admin/page_parts_controller.rb index d591dd83..508034f2 100644 --- a/app/controllers/admin/page_parts_controller.rb +++ b/app/controllers/admin/page_parts_controller.rb @@ -54,6 +54,7 @@ class Admin::PagePartsController < ApplicationController end def update + binding.pry @part = PagePart.find(params[:id]) params[:page_part][:widget_field] = params[:page_part][:widget_field].zip( params[:page_part][:widget_field_type],params[:page_part][:widget_field_is_link] ) @@ -63,6 +64,17 @@ class Admin::PagePartsController < ApplicationController if params[:page_part][:module_app].blank? params[:page_part][:module_app] = nil end + + if params[:page_part][:tag].include?("nil") + params[:page_part][:tag] = [] + end + + if params[:page_part][:category].include?("nil") + params[:page_part][:category] = [] + end + + + if @part.update_attributes(params[:page_part]) set_children_sub_menu(@part) if @part.public_r_tag && @part.public_r_tag.eql?('sub_menu') flash.now[:notice] = t('update.success.content') diff --git a/app/controllers/admin/pages_controller.rb b/app/controllers/admin/pages_controller.rb index f2de0c9e..46e2ecd0 100644 --- a/app/controllers/admin/pages_controller.rb +++ b/app/controllers/admin/pages_controller.rb @@ -97,6 +97,14 @@ helper Admin::PagePartsHelper params[:page][:frontend_field] = params[:page][:frontend_field].zip( params[:page][:frontend_field_type] ) if params[:page][:frontend_field] params[:page][:frontend_field_type] = nil + if params[:page][:tag].include?("nil") + params[:page][:tag] = [] + end + + if params[:page][:category].include?("nil") + params[:page][:category] = [] + end + if @item.update_attributes(params[:page]) flash[:notice] = t('update.success.page') respond_to do |format| diff --git a/app/controllers/default_widget_controller.rb b/app/controllers/default_widget_controller.rb index 956a2a53..0963b18a 100644 --- a/app/controllers/default_widget_controller.rb +++ b/app/controllers/default_widget_controller.rb @@ -10,31 +10,60 @@ class DefaultWidgetController< OrbitWidgetController @page_part.module_app.enable_frontend? end + def query_for_default_widget + ori_class_name = @default_widget["query"].split('.')[0] + result = nil + result_objects = nil + if !params["tag_id"].blank? + @tags = Tag.find(params["tag_id"]) rescue nil + result_ids = nil + if params["category_id"].blank? + result_ids = @tags.collect{|tag| tag.send(ori_class_name.downcase.pluralize).entries.collect{|t| t.id}}.flatten + else + result_ids = @tags.collect{|tag| tag.send(ori_class_name.downcase.pluralize).where(:category_id=>params["category_id"]).available_for_lang(I18n.locale).can_display.collect{|t| t.id}}.flatten + end + result = eval("#{ori_class_name}.where(:id=>result_ids)") + elsif params["category_id"].blank? + result = eval(@default_widget["query"]) + else + result = eval("#{ori_class_name}.where(:category_id=>params['category_id'])") + end + result.available_for_lang(I18n.locale).can_display + end + def default_widget @tag_class = nil - - @default_widget = @page_part.module_app.get_default_widget - @widget_image_field = @default_widget["image"] || @default_widget[:image] - data_limit = case @page_part - when Page - @fields = @page_part.frontend_field - @page_part.frontend_data_count.is_a?(Fixnum) ? @page_part.frontend_data_count : (@page_part.frontend_data_count.to_i rescue 3) - when PagePart - @fields = @page_part.widget_field - @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3) - end - @data = eval(@default_widget["query"]).limit(data_limit).includes(@widget_image_field).desc(:created_at) - case params[:type] - when "typeA" - @tag_class = 'default_widget_typeA' - render "typeA" - when /typeB_/ - @tag_class = "default_widget_#{params[:type]}" - render "typeB" - when "typeC" - @tag_class = 'default_widget_typeC' - render "typeC" - end + @default_widget = @page_part.module_app.get_default_widget + @widget_image_field = @default_widget["image"] || @default_widget[:image] + case @page_part + when Page + @fields = @page_part.frontend_field + @data_limit = @page_part.frontend_data_count.is_a?(Fixnum) ? @page_part.frontend_data_count : (@page_part.frontend_data_count.to_i rescue 3) + @paginate = true + @page_id = @page_part.id + when PagePart + @fields = @page_part.widget_field + @data_limit = @page_part.widget_data_count.is_a?(Fixnum) ? @page_part.widget_data_count : (@page_part.widget_data_count.to_i rescue 3) + end + if @paginate + @data = query_for_default_widget.includes(@widget_image_field).page(params[:page_main]).per(@data_limit) + else + @data = query_for_default_widget.limit(@data_limit).includes(@widget_image_field) + end + case params[:type] + when "typeA" + @tag_class = 'default_widget_typeA' + @partial = "typeA" + render "typeA" + when /typeB_/ + @tag_class = "default_widget_#{params[:type]}" + @partial = "typeB" + render "typeB" + when "typeC" + @tag_class = 'default_widget_typeC' + @partial = "typeC" + render "typeC" + end end diff --git a/app/helpers/admin/page_parts_helper.rb b/app/helpers/admin/page_parts_helper.rb index 898b139e..f1cf1421 100644 --- a/app/helpers/admin/page_parts_helper.rb +++ b/app/helpers/admin/page_parts_helper.rb @@ -84,17 +84,17 @@ module Admin::PagePartsHelper def tag_checked_value(part,radio_value) if radio_value.blank? - (part.nil? || part[:tag].blank?)? true : false + (part.nil? || part[:tag].blank?) ? true : false else - part and (part[:tag] == radio_value.to_s) ? true : false + part and (part[:tag].include?(radio_value.to_s)) ? true : false end end def category_checked_value(part,radio_value) if radio_value.blank? - (part.nil? || part[:category].blank?)? true : false + (part.nil? || part[:category].blank?) ? true : false else - part and (part[:category] == radio_value.to_s) ? true : false + part and (part[:category].include?(radio_value.to_s)) ? true : false end end @@ -117,6 +117,19 @@ module Admin::PagePartsHelper end end + def nil_checkbox_button(part,tag_or_cate) + field_name = case part + when PagePart + 'page_part' + when Page + 'page' + end + content_tag :label,:class=>"radio inline" do + op = check_box_tag("#{field_name}[#{tag_or_cate}][]", 'nil' , eval("#{tag_or_cate}_checked_value(part,'')"),:class=>'select_all') + op << I18n.t("default_widget.no_value") + end + end + def nil_radio_button(part,tag_or_cate) field_name = case part when PagePart diff --git a/app/models/page.rb b/app/models/page.rb index bc257eaa..545105fd 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -8,8 +8,8 @@ class Page < Item field :content, localize: true field :app_frontend_url field :theme_id, :type => BSON::ObjectId, :default => nil - field :category, :default => '' - field :tag, :default => '' + field :category,type: Array, :default => [] + field :tag, type: Array,:default => [] field :view_count, :type => Integer, :default => 0 field :page_title, localize: true diff --git a/app/models/page_part.rb b/app/models/page_part.rb index b48c8060..4b0d4a8c 100644 --- a/app/models/page_part.rb +++ b/app/models/page_part.rb @@ -11,6 +11,8 @@ class PagePart field :public_r_tag_option, :default => nil field :page_title, localize: true field :widget_path + field :category,type: Array, :default => [] + field :tag, type: Array,:default => [] field :widget_style field :widget_field , :type => Array diff --git a/app/views/admin/page_parts/_widget_data_source_category.html.erb b/app/views/admin/page_parts/_widget_data_source_category.html.erb index d088c645..c3e4c21d 100644 --- a/app/views/admin/page_parts/_widget_data_source_category.html.erb +++ b/app/views/admin/page_parts/_widget_data_source_category.html.erb @@ -10,9 +10,9 @@
<% if @categories %>
- <%= nil_radio_button(object,:category) %> + <%= nil_checkbox_button(object,:category) %> <%= content_tag_for(:label, @categories,:class=>"radio inline") do |category|%> - <%= radio_button_tag("#{field_name}[category]", category.id, category_checked_value(object,category.id) )%> + <%= check_box_tag("#{field_name}[category][]", category.id, category_checked_value(object,category.id),:class=>'select_option' )%> <%= category.title%> <% end %>
diff --git a/app/views/admin/page_parts/_widget_data_source_tag.html.erb b/app/views/admin/page_parts/_widget_data_source_tag.html.erb index bcb3e9c3..c067aede 100644 --- a/app/views/admin/page_parts/_widget_data_source_tag.html.erb +++ b/app/views/admin/page_parts/_widget_data_source_tag.html.erb @@ -10,9 +10,9 @@
<% if @categories %>
- <%= nil_radio_button(object,:tag) %> + <%= nil_checkbox_button(object,:tag) %> <%= content_tag_for(:label, @tags,:class=>"radio inline") do |tag|%> - <%= radio_button_tag("#{field_name}[tag]", tag.id, tag_checked_value(object,tag.id) ) %> + <%= check_box_tag("#{field_name}[tag][]", tag.id, tag_checked_value(object,tag.id),:class=>'select_option' ) %> <%= tag[I18n.locale]%> <% end if @tags%>
diff --git a/app/views/admin/page_parts/edit.html.erb b/app/views/admin/page_parts/edit.html.erb index 907cc7eb..198e4255 100644 --- a/app/views/admin/page_parts/edit.html.erb +++ b/app/views/admin/page_parts/edit.html.erb @@ -2,4 +2,10 @@ <%= render 'admin/items/site_map_left_bar' %> <% end -%> -<%= render 'edit' %> \ No newline at end of file + + + + +<%= render 'edit' %> + +<%=javascript_include_tag "inc/update_cates_and_tags.js" %> \ No newline at end of file diff --git a/app/views/admin/pages/edit.html.erb b/app/views/admin/pages/edit.html.erb index 8bd76634..8c3c0711 100644 --- a/app/views/admin/pages/edit.html.erb +++ b/app/views/admin/pages/edit.html.erb @@ -4,3 +4,4 @@ <% end -%> <%= render 'edit' %> +<%=javascript_include_tag "inc/update_cates_and_tags.js" %> \ No newline at end of file diff --git a/lib/tasks/migrate.rake b/lib/tasks/migrate.rake index f3b16f16..9770f151 100644 --- a/lib/tasks/migrate.rake +++ b/lib/tasks/migrate.rake @@ -492,4 +492,24 @@ namespace :migrate do pp.save end end + + task :convert_cate_tag_type => :environment do + [Page,PagePart].each do |obj| + obj.all.each do |pp| + if pp.category.blank? + pp.category = [] + else + pp.category = [pp.category] + end + + if pp.tag.blank? + pp.tag = [] + else + pp.tag = [pp.tag] + end + pp.save + end + end + end + end