Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 10 additions & 35 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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

Expand Down
6 changes: 2 additions & 4 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 10 additions & 15 deletions app/views/resources/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
<div class="container-fluid search-curriculum-page resources-page">
<div class="row">
<div class="col-md-12 col-lg-8 width">
<div class="col-md-9 col-lg-8 width">
<div class="entry-content">
<% if params[:kind] == "Scholarship" %>
<h2 class="normal">Scholarship</h2>
<% else %>
<h2 class="normal">Resources</h2>
<% end %>
<h2 class="normal">Resources</h2>
</div>
</div>
<div class="col-md-3 col-lg-4 width">
<div class="entry-content">
<%= link_to "New Resource", new_resource_path, class: "btn btn-success mb-3" %>
</div>
</div>
</div>
Expand All @@ -15,9 +16,7 @@
<%= form_tag resources_path, method: 'get' do |f| %>
<div class="search-wrapper">
<div class="search-field-row" >
<% if params[:kind] == "Scholarship" %>
<input type="hidden" name="kind" value="<%= params[:kind] %>" />
<% end %>
<input type="hidden" name="kind" value="<%= params[:kind] %>" />

<%= text_field_tag :query, params[:query], class: 'search',
placeholder: ' Enter keywords here...' %>
Expand All @@ -26,11 +25,7 @@
</div>
</div>
</div>
<% if params[:kind] != "Scholarship" %>
<div class="clear-lnk">
<%= link_to 'Clear All', resources_path %>
</div>
<% end %>
<%= link_to 'Clear All', resources_path %>

</div>
</div><!-- search row -->
Expand All @@ -40,7 +35,7 @@
<div class="filter-wrapper">
<% if params[:kind] != "Scholarship" %>
<div class="small-gray-title">Filter By:</div>
<%= render 'shared/sortable_fields_btn', sortable_fields: @sortable_fields %>
<%= render 'shared/sortable_fields_btn', sortable_fields: Resource::KINDS - ["Story"] %>
</div>
<% end %>
</div>
Expand Down
85 changes: 49 additions & 36 deletions app/views/resources/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,44 +1,57 @@
<h2 class="normal"><%= "Share a #{@resource.kind.titleize}" %></h2>
<%= form_for @resource, as: :resource, url: resources_path do |f| %>
<h2 class="normal"><%= "New Resource" %></h2>

<%= simple_form_for @resource, as: :resource, url: resources_path do |f| %>
<%= render 'shared/errors', resource: @resource if @resource.errors.any? %>
<div class="form-group">
<br>
<%= f.label 'Title:', class: 'bold' %>
<%= f.text_field :title, placeholder: 'enter a title' %>
<%= f.text_area :text, placeholder: 'share a story with us' %>

<div class="row mb-3">
<div class="col-md-8">
<%= f.input :title,
placeholder: "enter a title",
wrapper: false,
input_html: { class: "form-control w-100" } %>
</div>
</div>
<%= f.hidden_field :kind %>
<h4 class="normal">Check all that is related to this story (optional)</h4>
<div class="form-group">
<%= f.label 'Age:', class: 'bold' %>
<%= f.fields_for :categorizable_items do |ff| %>
<%= render 'categorizable_item_fields', f: ff %>
<% end %>
</div>
<div class="form-group">
<br>
<%= 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' %>
<div class="row mb-3">
<div class="col-md-1">
<%= f.input :kind, as: :select, collection: ["Toolkit","Form","Template","Handout", "Story"] %>
</div>
<div class="col-md-4">
<%= f.input :sectors, as: :select, collection: @sectors %>
</div>
<div class="col-md-4">
<%= f.input :url, as: :text, input_html: { rows: 1, class: "form-control w-100" } %>
</div>
<div class="col-md-3">
<%= 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" } %>
</div>
</div>
<div class="form-group">
<br>
<%= f.label 'Service Population:', class: 'bold' %>
<%= f.fields_for :sectorable_items do |ff| %>
<%= render 'shared/sectorable_item_fields', f: ff %>
<% end %>
<div class="row mb-3">
<div class="col-md-4">
<%= f.input :text, as: :text, input_html: { rows: 5 } %>
</div>
<div class="col-md-8">
<%= f.input :attachment, as: :file %>
</div>
</div>
<div class="form-group">
<br>
<%= f.fields_for :images do |image| %>
<%= render 'shared/image_fields', f: image %>
<div class="links">
<%= link_to_add_association 'add image', f, :images, partial: 'shared/image_fields' %>
</div>
<% end %>
<div class="row">
<div class="col-md-4">
<%= f.input :featured, as: :boolean,
label: "Feature on the dashboard?" %>
</div>
<div class="col-md-8">
<%= f.input :inactive, as: :boolean,
input_html: { class: "form-check-input" },
wrapper_html: { class: "form-check d-block text-start" } %>
</div>
</div>




<div class="form-group">
<br>
<%= link_to 'Cancel', root_path, class: 'btn cancel-btn' %>
Expand Down
2 changes: 0 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
post :bulk_create
end
end
resources :resources

get 'stories', to: 'resources#stories'

Expand Down Expand Up @@ -89,7 +88,6 @@
resources :authentications, only: [:create]
resources :workshops
resources :quotes
resources :resources
resources :bookmarks do
resources :annotations
end
Expand Down
15 changes: 3 additions & 12 deletions spec/factories/resources.rb
Original file line number Diff line number Diff line change
@@ -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
end
32 changes: 3 additions & 29 deletions spec/models/resource_spec.rb
Original file line number Diff line number Diff line change
@@ -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
end