diff --git a/app/controllers/resources_controller.rb b/app/controllers/resources_controller.rb index 7a52d9ab3..88cd6a9e0 100644 --- a/app/controllers/resources_controller.rb +++ b/app/controllers/resources_controller.rb @@ -3,7 +3,7 @@ def index @resources = current_user.curriculum(Resource).by_created.search(params). paginate(page: params[:page], per_page: 6) - load_sortable_fields + @sortable_fields = Resource::KINDS respond_to do |format| format.html @@ -15,10 +15,12 @@ def stories end def new - @resource = Resource.new(type: params[:type]) - load_age_ranges - load_sectors - load_images + @resource = Resource.new + @sectors = Sector.pluck(:name, :id) + end + + def edit + @resource = Resource.find(resource_id_param).decorate end def show @@ -30,17 +32,16 @@ def show def create @resource = current_user.resources.build(resource_params) if @resource.save - flash[:alert] = "#{@resource.type} has been submitted." - redirect_to root_path + redirect_to resources_path else - flash[:error] = "Unable to save #{@resource.type.titleize}" + flash[:error] = "Unable to save #{@resource.title.titleize}" render :new end end def search process_search - load_sortable_fields + @sortable_fields = Resource::KINDS.dup.delete("Story") render :index end @@ -81,32 +82,6 @@ def load_forms end end - def load_age_ranges - Metadatum.find_by(name: 'AgeRange').categories.each do |category| - @resource.categorizable_items.build(category: category) - end - end - - def load_sectors - Sector.all.each do |sector| - @resource.sectorable_items.build(sector: sector) - end - end - - - def load_images - @resource.images.build - end - - def load_sortable_fields - @sortable_fields = [ - 'Toolkit', - 'Form', - 'Template', - 'Handout' - ] - end - def search_params params[:search] end diff --git a/app/models/resource.rb b/app/models/resource.rb index 4aa830ab2..9f4f1aa82 100644 --- a/app/models/resource.rb +++ b/app/models/resource.rb @@ -2,7 +2,7 @@ class Resource < ApplicationRecord # Associations belongs_to :user belongs_to :workshop, optional: true - belongs_to :windows_type + belongs_to :windows_type, optional: true has_many :images, as: :owner, dependent: :destroy has_many :categorizable_items, dependent: :destroy, as: :categorizable has_many :categories, through: :categorizable_items @@ -22,6 +22,8 @@ class Resource < ApplicationRecord scope :recent, -> { for_search.by_created } validates :title, presence: true + validates :kind, presence: true + attribute :inactive, :boolean, default: false # Nested Attributes accepts_nested_attributes_for :categorizable_items, @@ -37,6 +39,8 @@ class Resource < ApplicationRecord accepts_nested_attributes_for :attachments, reject_if: :all_blank, allow_destroy: true accepts_nested_attributes_for :form, reject_if: :all_blank, allow_destroy: true + KINDS = ['Toolkit', 'Form', 'Template', 'Handout', 'Story'] + # Search Cop include SearchCop diff --git a/app/models/user.rb b/app/models/user.rb index a04049d65..ed45b61fd 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -120,11 +120,9 @@ def permissions_list end def curriculum(klass = Workshop) - results = klass.joins(:windows_type) - results = results.where('windows_types.name IN (?) and inactive is false', permissions_list) - + results = klass.where(inactive: false) results = results.where(kind: ['Template','Handout', 'Scholarship', - 'Toolkit', 'Form', 'Resource']) if klass == Resource + 'Toolkit', 'Form', 'Resource', 'Story']) if klass == Resource results end diff --git a/app/views/resources/index.html.erb b/app/views/resources/index.html.erb index a7fef692c..abb217722 100644 --- a/app/views/resources/index.html.erb +++ b/app/views/resources/index.html.erb @@ -1,12 +1,13 @@