diff --git a/app/controllers/story_ideas_controller.rb b/app/controllers/story_ideas_controller.rb index b2f674f91..296feba3f 100644 --- a/app/controllers/story_ideas_controller.rb +++ b/app/controllers/story_ideas_controller.rb @@ -35,6 +35,8 @@ def create @story_idea.updated_by = current_user authorize! @story_idea + assign_categories(@story_idea) + if @story_idea.save NotificationServices::CreateNotification.call( noticeable: @story_idea, @@ -59,6 +61,8 @@ def update @story_idea.updated_by = current_user authorize! @story_idea + assign_categories(@story_idea) + if @story_idea.update(story_idea_params.except(:images)) flash[:notice] = "StoryIdea was successfully updated." if allowed_to?(:index?, StoryIdea) @@ -90,6 +94,15 @@ def set_form_variables @users = User.active.includes(:person) @users = @users.or(User.where(id: @story_idea.created_by_id)) if @story_idea&.created_by_id @users = @users.distinct.order("people.first_name, people.last_name") + @categories_grouped = + Category + .includes(:category_type) + .published + .order(:position, :name) + .group_by(&:category_type) + .select { |type, _| type.nil? || type.published? } + .sort_by { |type, _| type&.name.to_s.downcase } + @sectors = Sector.published.order(:name) @story_idea.build_primary_asset if @story_idea.primary_asset.blank? @story_idea.gallery_assets.build end @@ -100,6 +113,14 @@ def set_story_idea @story_idea = StoryIdea.find(params[:id]) end + def assign_categories(story_idea) + selected_category_ids = Array(params[:story_idea][:category_ids]).reject(&:blank?).map(&:to_i) + story_idea.categories = Category.where(id: selected_category_ids) + + selected_sector_ids = Array(params[:story_idea][:sector_ids]).reject(&:blank?).map(&:to_i) + story_idea.sectors = Sector.where(id: selected_sector_ids) + end + def story_idea_params params.require(:story_idea).permit( :title, :body, :youtube_url, @@ -107,10 +128,10 @@ def story_idea_params :windows_type_id, :organization_id, :workshop_id, :external_workshop_title, :created_by_id, :updated_by_id, story_populations: [], + category_ids: [], + sector_ids: [], primary_asset_attributes: [ :id, :file, :_destroy ], - gallery_assets_attributes: [ :id, :file, :_destroy ], - categorizable_items_attributes: [ :id, :category_id, :_destroy ], - category_ids: [] + gallery_assets_attributes: [ :id, :file, :_destroy ] ) end end diff --git a/app/models/story_idea.rb b/app/models/story_idea.rb index 1feb094db..c7a15c8f5 100644 --- a/app/models/story_idea.rb +++ b/app/models/story_idea.rb @@ -12,6 +12,7 @@ class StoryIdea < ApplicationRecord belongs_to :workshop, optional: true has_many :bookmarks, as: :bookmarkable, dependent: :destroy has_many :categorizable_items, dependent: :destroy, inverse_of: :categorizable, as: :categorizable + has_many :sectorable_items, dependent: :destroy, inverse_of: :sectorable, as: :sectorable has_many :notifications, as: :noticeable, dependent: :destroy has_many :stories # Asset associations @@ -22,6 +23,7 @@ class StoryIdea < ApplicationRecord has_many :assets, as: :owner, dependent: :destroy # has_many through has_many :categories, through: :categorizable_items + has_many :sectors, through: :sectorable_items # Validations validates :created_by_id, presence: true diff --git a/app/views/story_ideas/_form.html.erb b/app/views/story_ideas/_form.html.erb index f2c04b8c1..7bc196c72 100644 --- a/app/views/story_ideas/_form.html.erb +++ b/app/views/story_ideas/_form.html.erb @@ -155,6 +155,101 @@ <% end %> + + +