From 5a4b3088f9b4c75d869a169d204109291df31fdc Mon Sep 17 00:00:00 2001 From: Chihyu Date: Mon, 6 Apr 2020 16:36:42 +0800 Subject: [PATCH] fix data_count error --- .../admin/activities_controller.rb | 99 +++++++++++++++++-- app/models/activity.rb | 17 ++-- app/models/activity_file.rb | 10 ++ app/models/activity_intro.rb | 2 + config/locales/zh_tw.yml | 2 +- 5 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 app/models/activity_file.rb create mode 100644 app/models/activity_intro.rb diff --git a/app/controllers/admin/activities_controller.rb b/app/controllers/admin/activities_controller.rb index 2c048fa..2bc59ea 100644 --- a/app/controllers/admin/activities_controller.rb +++ b/app/controllers/admin/activities_controller.rb @@ -2,11 +2,12 @@ class Admin::ActivitiesController < OrbitMemberController layout "member_plugin" include Admin::PersonalActivitiesHelper - before_action :set_activity, only: [:edit, :update, :destroy] - #before_action :set_plugin + before_action :set_activity, only: [:show, :edit, :update, :destroy] + before_action :set_plugin + before_action :get_settings,:only => [:new, :edit, :setting] - #before_action :need_access_right - #before_action :allow_admin_only, :only => [:index, :setting] + before_action :need_access_right + before_action :allow_admin_only, :only => [:index, :setting] def index @activities = Activity.order_by(:year => 'desc').page(params[:page]).per(10) @@ -27,22 +28,56 @@ class Admin::ActivitiesController < OrbitMemberController def create if !activity_params['member_profile_id'].blank? @member = MemberProfile.find(activity_params['member_profile_id']) rescue nil + @activity = Activity.new(activity_params) + @activity.save + + if params[:desktop] == "true" + render json: {"data" => get_paper_list}.to_json + else + redirect_to params['referer_url'] + end + elsif !params[:author_members].blank? - activity_params['member_profile_id'] = params[:author_members] + + params[:author_members].each do |author_member| + activity_params['member_profile_id'] = author_member + @activity = Activity.new(activity_params) + @activity.save + + if params[:desktop] == "true" + render json: {"data" => get_paper_list}.to_json + end + end + redirect_to params['referer_url'] else - activity_params['member_profile_id'] = current_user.member_profile_id + activity_params['member_profile_id'] = User.find(current_user.id).member_profile_id + @activity = Activity.new(activity_params) + @activity.save + if params[:desktop] == "true" + render json: {"data" => get_paper_list}.to_json + end + redirect_to params['referer_url'] end - @activity = Activity.new(activity_params) - @activity.save - redirect_to params['referer_url'] + end + def edit + @member = @activity.member_profile rescue nil + if params[:desktop] + render :layout => false + end end def update + @member = @activity.member_profile rescue nil @activity.update_attributes(activity_params) - redirect_to params['referer_url'] + @activity.save + if params[:desktop] == "true" + render json: {"data" => get_paper_list}.to_json + else + redirect_to params['referer_url'] + end end def destroy @@ -54,6 +89,7 @@ class Admin::ActivitiesController < OrbitMemberController end end + def analysis end @@ -105,6 +141,45 @@ class Admin::ActivitiesController < OrbitMemberController end end + def toggle_hide + if params[:ids] + @activities = Activity.any_in(_id: params[:ids]) + @activities.each do |activity| + activity.is_hidden = params[:disable] + activity.save + end + end + render json: {"success"=>true} + end + + def setting + end + + def frontend_setting + @member = MemberProfile.find_by(:uid=>params['uid']) rescue nil + @intro = ActivityIntro.find_by(:member_profile_id=>@member.id) rescue nil + @intro = @intro.nil? ? ActivityIntro.new({:member_profile_id=>@member.id}) : @intro + end + + def update_frontend_setting + @member = MemberProfile.find(intro_params['member_profile_id']) rescue nil + @intro = ActivityIntro.find_by(:member_profile_id=>@member.id) rescue nil + @intro = @intro.nil? ? ActivityIntro.new({:member_profile_id=>@member.id}) : @intro + @intro.update_attributes(intro_params) + @intro.save + redirect_to URI.encode('/admin/members/'+@member.to_param+'/activity') + end + + def get_settings + end + + def set_plugin + @plugin = OrbitApp::Plugin::Registration.all.select{|plugin| plugin.app_name.eql? 'Activity'}.first + @module_app = ModuleApp.where(:key=>'personal_activity').first + @module_app = ModuleApp.create(:key=>'personal_activity',:title=>'PersonalActivity','frontend_enable'=>true) if @module_app.nil? + @categories = @module_app.categories + end + private def set_activity @@ -122,4 +197,8 @@ class Admin::ActivitiesController < OrbitMemberController def activity_params params.require(:activity).permit! rescue nil end + + def intro_params + params.require(:activity_intro).permit! rescue nil + end end diff --git a/app/models/activity.rb b/app/models/activity.rb index 4e740e8..464af9e 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -4,8 +4,9 @@ class Activity #include Mongoid::Attributes::Dynamic #include Admin::PersonalActivitiesHelper include OrbitModel::Status - include MemberHelper + #include MemberHelper include Slug + include OrbitCategory::Categorizable belongs_to :member_profile # Language @@ -18,11 +19,12 @@ class Activity field :activity_start_date, type: DateTime field :activity_end_date, type: DateTime field :note, type: String + field :url # paginates_per 10 has_many :activity_files, :autosave => true, :dependent => :destroy - belongs_to :activity_category + #belongs_to :activity_category accepts_nested_attributes_for :activity_files, :allow_destroy => true before_validation :add_http @@ -52,7 +54,7 @@ class Activity pd_title = [] fields_to_show.each do |t| - if (self.fields[t].type.to_s == "String" || self.fields[t].type.to_s == "Object" rescue false) + if (self.fields[t].type.to_s == "String" rescue false) fields_to_remove << t if (datas.where(t.to_sym.ne => nil, t.to_sym.ne => "").count == 0 rescue false) else fields_to_remove << t if (datas.where(t.to_sym.ne => nil).count == 0 rescue false) @@ -64,25 +66,20 @@ class Activity fields_to_show = fields_to_show - fields_to_remove - plugin_datas = datas.sort_for_frontend.collect.with_index do |p, index| + plugin_datas = datas.sort_for_frontend.collect do |p| pd_data = [] fields_to_show.collect do |t| if t == "activity_title" pd_data << { "data_title" => "#{p.send(t)}" } - elsif t == "activity_category" - pd_data << {"data_title" => (p.activity_category.title rescue "")} else pd_data << { "data_title" => p.send(t) } end end { - "pd_datas" => pd_data, - "type-sort" => (p.activity_category.sort_position.to_i rescue -1), - "sort-index" => index + "pd_datas" => pd_data } end - plugin_datas = plugin_datas.sort_by{|pd| [pd["type-sort"], pd["sort-index"]]} return [pd_title,plugin_datas] end diff --git a/app/models/activity_file.rb b/app/models/activity_file.rb new file mode 100644 index 0000000..1c827aa --- /dev/null +++ b/app/models/activity_file.rb @@ -0,0 +1,10 @@ +class ActivityFile + include Mongoid::Document + include Mongoid::Timestamps + mount_uploader :file, AssetUploader + field :description, localize: true + field :should_destroy, :type => Boolean + field :title, localize: true + + belongs_to :activity + end \ No newline at end of file diff --git a/app/models/activity_intro.rb b/app/models/activity_intro.rb new file mode 100644 index 0000000..32e4d80 --- /dev/null +++ b/app/models/activity_intro.rb @@ -0,0 +1,2 @@ +class ActivityIntro < PersonalPluginIntro +end \ No newline at end of file diff --git a/config/locales/zh_tw.yml b/config/locales/zh_tw.yml index 2468422..a31fc1c 100644 --- a/config/locales/zh_tw.yml +++ b/config/locales/zh_tw.yml @@ -18,7 +18,7 @@ zh_tw: description : "描述" upload: "上傳" frontend: - writings: "活動前台" + activities: "活動前台" create_success : "新增完成!!" update_success : "更新完成!!"