diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 9f1d13ae..9052ed76 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -68,12 +68,12 @@ class CategoriesController < ApplicationController private def set_school redirect_to root_path and return false unless params.include?(:school_id) - @school = School.find(params[:school_id]) + @school = School.friendly.find(params[:school_id]) redirect_to root_path and return false if @school.nil? end def set_category - @category = Category.find(params[:id]) + @category = Category.friendly.find(params[:id]) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index eff3fdbc..fce8e41f 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -65,7 +65,7 @@ class QuestionsController < ApplicationController private def set_school redirect_to root_path and return false unless params.include?(:school_id) - @school = School.find(params[:school_id]) + @school = School.friendly.find(params[:school_id]) redirect_to root_path and return false if @school.nil? end diff --git a/app/controllers/recipient_lists_controller.rb b/app/controllers/recipient_lists_controller.rb index b327716d..1b7a9465 100644 --- a/app/controllers/recipient_lists_controller.rb +++ b/app/controllers/recipient_lists_controller.rb @@ -50,7 +50,7 @@ class RecipientListsController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_school - @school = School.find(params[:school_id]) + @school = School.friendly.find(params[:school_id]) end def set_recipient_list diff --git a/app/controllers/recipients_controller.rb b/app/controllers/recipients_controller.rb index 36ccb4f4..8206f9e7 100644 --- a/app/controllers/recipients_controller.rb +++ b/app/controllers/recipients_controller.rb @@ -72,7 +72,7 @@ class RecipientsController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_school - @school = School.find(params[:school_id]) + @school = School.friendly.find(params[:school_id]) end # Use callbacks to share common setup or constraints between actions. diff --git a/app/controllers/schedules_controller.rb b/app/controllers/schedules_controller.rb index 9f3d49bc..9d77214b 100644 --- a/app/controllers/schedules_controller.rb +++ b/app/controllers/schedules_controller.rb @@ -50,7 +50,7 @@ class SchedulesController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_school - @school = School.find(params[:school_id]) + @school = School.friendly.find(params[:school_id]) end def set_schedule diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index 41593344..941f6e54 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -65,7 +65,7 @@ class SchoolsController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_school - @school = School.find(params[:id] || params[:school_id]) + @school = School.friendly.find(params[:id] || params[:school_id]) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/models/category.rb b/app/models/category.rb index 9c115de5..f83c1ed9 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -9,6 +9,9 @@ class Category < ApplicationRecord scope :for_parent, -> (category=nil) { where(parent_category_id: category.try(:id)) } + include FriendlyId + friendly_id :name, :use => [:slugged] + def path p = self items = [p] diff --git a/app/models/school.rb b/app/models/school.rb index 8b297c97..96621615 100644 --- a/app/models/school.rb +++ b/app/models/school.rb @@ -7,4 +7,7 @@ class School < ApplicationRecord validates :name, presence: true + include FriendlyId + friendly_id :name, :use => [:slugged] + end diff --git a/db/migrate/20170316194058_add_slug_to_school.rb b/db/migrate/20170316194058_add_slug_to_school.rb new file mode 100644 index 00000000..b4e273c6 --- /dev/null +++ b/db/migrate/20170316194058_add_slug_to_school.rb @@ -0,0 +1,6 @@ +class AddSlugToSchool < ActiveRecord::Migration[5.0] + def change + add_column :schools, :slug, :string + add_index :schools, :slug, unique: true + end +end diff --git a/db/migrate/20170316194122_add_slug_to_category.rb b/db/migrate/20170316194122_add_slug_to_category.rb new file mode 100644 index 00000000..f0127739 --- /dev/null +++ b/db/migrate/20170316194122_add_slug_to_category.rb @@ -0,0 +1,6 @@ +class AddSlugToCategory < ActiveRecord::Migration[5.0] + def change + add_column :categories, :slug, :string + add_index :categories, :slug, unique: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 6ea0561a..3975d75f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170316154053) do +ActiveRecord::Schema.define(version: 20170316194122) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -40,6 +40,8 @@ ActiveRecord::Schema.define(version: 20170316154053) do t.integer "parent_category_id" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "slug" + t.index ["slug"], name: "index_categories_on_slug", unique: true, using: :btree end create_table "districts", force: :cascade do |t| @@ -141,6 +143,8 @@ ActiveRecord::Schema.define(version: 20170316154053) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "description" + t.string "slug" + t.index ["slug"], name: "index_schools_on_slug", unique: true, using: :btree end create_table "users", force: :cascade do |t| diff --git a/lib/tasks/data.rake b/lib/tasks/data.rake index 7f2058bd..07ad6954 100644 --- a/lib/tasks/data.rake +++ b/lib/tasks/data.rake @@ -143,7 +143,8 @@ namespace :data do bad_answers = {} year = '2016' ['student_responses', 'teacher_responses'].each do |file| - target_group = Question.target_groups["for_#{file.split('_')[0]}s"] + recipients = file.split('_')[0] + target_group = Question.target_groups["for_#{recipients}s"] csv_string = File.read(File.expand_path("../../../data/#{file}_#{year}.csv", __FILE__)) csv = CSV.parse(csv_string, :headers => true) csv.each do |row| @@ -158,6 +159,11 @@ namespace :data do next end + recipient_list = school.recipient_lists.find_by_name("#{recipients.titleize} List") + if recipient_list.nil? + school.recipient_lists.create(name: "#{recipients.titleize} List") + end + respondent_id = row['RespondentID'] recipient_id = respondent_map[respondent_id] if recipient_id.present? @@ -168,6 +174,8 @@ namespace :data do ) respondent_map[respondent_id] = recipient.id end + recipient_list.recipient_id_array << recipient.id + recipient_list.save! row.each do |key, value| next if value.nil? or key.nil? diff --git a/spec/views/recipients/index.html.erb_spec.rb b/spec/views/recipients/index.html.erb_spec.rb index 82250e7b..a6063dcf 100644 --- a/spec/views/recipients/index.html.erb_spec.rb +++ b/spec/views/recipients/index.html.erb_spec.rb @@ -16,7 +16,7 @@ RSpec.describe "recipients/index", type: :view do :home_language_id => 2, :income => "Income", :opted_out => false, - :school_id => @school.to_param + :school_id => @school.id ), Recipient.create!( :name => "Name", @@ -27,7 +27,7 @@ RSpec.describe "recipients/index", type: :view do :home_language_id => 2, :income => "Income", :opted_out => false, - :school_id => @school.to_param + :school_id => @school.id ) ]) end @@ -42,6 +42,6 @@ RSpec.describe "recipients/index", type: :view do assert_select "tr>td", :text => 2.to_s, :count => 2 assert_select "tr>td", :text => "Income".to_s, :count => 2 assert_select "tr>td", :text => false.to_s, :count => 2 - assert_select "tr>td", :text => @school.to_param, :count => 2 + assert_select "tr>td", :text => @school.id.to_s, :count => 2 end end