adding slugs to schools and categories

pull/1/head
Jared Cosulich 9 years ago
parent 683fc31c5d
commit 1db61cd3e5

@ -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.

@ -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

@ -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

@ -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.

@ -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

@ -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.

@ -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]

@ -7,4 +7,7 @@ class School < ApplicationRecord
validates :name, presence: true
include FriendlyId
friendly_id :name, :use => [:slugged]
end

@ -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

@ -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

@ -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|

@ -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?

@ -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

Loading…
Cancel
Save