mirror of
https://github.com/edcommonwealth/sqm-dashboards.git
synced 2026-03-07 21:48:16 -08:00
Add slug to district
This commit is contained in:
parent
886f94a60c
commit
71ad999dd0
9 changed files with 132 additions and 22 deletions
|
|
@ -66,7 +66,7 @@ class DistrictsController < ApplicationController
|
||||||
private
|
private
|
||||||
# Use callbacks to share common setup or constraints between actions.
|
# Use callbacks to share common setup or constraints between actions.
|
||||||
def set_district
|
def set_district
|
||||||
@district = District.find(params[:id])
|
@district = District.find_by_slug(params[:id])
|
||||||
end
|
end
|
||||||
|
|
||||||
# Never trust parameters from the scary internet, only allow the white list through.
|
# Never trust parameters from the scary internet, only allow the white list through.
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,12 @@ class District < ApplicationRecord
|
||||||
|
|
||||||
scope :alphabetic, -> { order(name: :asc) }
|
scope :alphabetic, -> { order(name: :asc) }
|
||||||
|
|
||||||
|
include FriendlyId
|
||||||
|
|
||||||
|
friendly_id :name, use: [:slugged]
|
||||||
|
|
||||||
|
before_save do
|
||||||
|
self.slug ||= name.parameterize
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
class ConstructGraphRowPresenter
|
class ConstructGraphRowPresenter
|
||||||
|
|
||||||
def initialize(construct:, score:)
|
def initialize(construct:, score:)
|
||||||
@construct = construct
|
@construct = construct
|
||||||
@score = score
|
@score = score
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select name="district">
|
<select name="district">
|
||||||
<option value="<%= @district.name %>" selected><%= @district.name %></option>
|
<option value="<%= @district.slug %>" selected><%= @district.name %></option>
|
||||||
</select>
|
</select>
|
||||||
|
|
||||||
<select name="school">
|
<select name="school">
|
||||||
|
|
|
||||||
88
config/initializers/friendly_id.rb
Normal file
88
config/initializers/friendly_id.rb
Normal file
|
|
@ -0,0 +1,88 @@
|
||||||
|
# FriendlyId Global Configuration
|
||||||
|
#
|
||||||
|
# Use this to set up shared configuration options for your entire application.
|
||||||
|
# Any of the configuration options shown here can also be applied to single
|
||||||
|
# models by passing arguments to the `friendly_id` class method or defining
|
||||||
|
# methods in your model.
|
||||||
|
#
|
||||||
|
# To learn more, check out the guide:
|
||||||
|
#
|
||||||
|
# http://norman.github.io/friendly_id/file.Guide.html
|
||||||
|
|
||||||
|
FriendlyId.defaults do |config|
|
||||||
|
# ## Reserved Words
|
||||||
|
#
|
||||||
|
# Some words could conflict with Rails's routes when used as slugs, or are
|
||||||
|
# undesirable to allow as slugs. Edit this list as needed for your app.
|
||||||
|
config.use :reserved
|
||||||
|
|
||||||
|
config.reserved_words = %w(new edit index session login logout users admin
|
||||||
|
stylesheets assets javascripts images)
|
||||||
|
|
||||||
|
# ## Friendly Finders
|
||||||
|
#
|
||||||
|
# Uncomment this to use friendly finders in all models. By default, if
|
||||||
|
# you wish to find a record by its friendly id, you must do:
|
||||||
|
#
|
||||||
|
# MyModel.friendly.find('foo')
|
||||||
|
#
|
||||||
|
# If you uncomment this, you can do:
|
||||||
|
#
|
||||||
|
# MyModel.find('foo')
|
||||||
|
#
|
||||||
|
# This is significantly more convenient but may not be appropriate for
|
||||||
|
# all applications, so you must explicity opt-in to this behavior. You can
|
||||||
|
# always also configure it on a per-model basis if you prefer.
|
||||||
|
#
|
||||||
|
# Something else to consider is that using the :finders addon boosts
|
||||||
|
# performance because it will avoid Rails-internal code that makes runtime
|
||||||
|
# calls to `Module.extend`.
|
||||||
|
#
|
||||||
|
# config.use :finders
|
||||||
|
#
|
||||||
|
# ## Slugs
|
||||||
|
#
|
||||||
|
# Most applications will use the :slugged module everywhere. If you wish
|
||||||
|
# to do so, uncomment the following line.
|
||||||
|
#
|
||||||
|
# config.use :slugged
|
||||||
|
#
|
||||||
|
# By default, FriendlyId's :slugged addon expects the slug column to be named
|
||||||
|
# 'slug', but you can change it if you wish.
|
||||||
|
#
|
||||||
|
# config.slug_column = 'slug'
|
||||||
|
#
|
||||||
|
# When FriendlyId can not generate a unique ID from your base method, it appends
|
||||||
|
# a UUID, separated by a single dash. You can configure the character used as the
|
||||||
|
# separator. If you're upgrading from FriendlyId 4, you may wish to replace this
|
||||||
|
# with two dashes.
|
||||||
|
#
|
||||||
|
# config.sequence_separator = '-'
|
||||||
|
#
|
||||||
|
# ## Tips and Tricks
|
||||||
|
#
|
||||||
|
# ### Controlling when slugs are generated
|
||||||
|
#
|
||||||
|
# As of FriendlyId 5.0, new slugs are generated only when the slug field is
|
||||||
|
# nil, but if you're using a column as your base method can change this
|
||||||
|
# behavior by overriding the `should_generate_new_friendly_id` method that
|
||||||
|
# FriendlyId adds to your model. The change below makes FriendlyId 5.0 behave
|
||||||
|
# more like 4.0.
|
||||||
|
#
|
||||||
|
# config.use Module.new {
|
||||||
|
# def should_generate_new_friendly_id?
|
||||||
|
# slug.blank? || <your_column_name_here>_changed?
|
||||||
|
# end
|
||||||
|
# }
|
||||||
|
#
|
||||||
|
# FriendlyId uses Rails's `parameterize` method to generate slugs, but for
|
||||||
|
# languages that don't use the Roman alphabet, that's not usually sufficient.
|
||||||
|
# Here we use the Babosa library to transliterate Russian Cyrillic slugs to
|
||||||
|
# ASCII. If you use this, don't forget to add "babosa" to your Gemfile.
|
||||||
|
#
|
||||||
|
# config.use Module.new {
|
||||||
|
# def normalize_friendly_id(text)
|
||||||
|
# text.to_slug.normalize! :transliterations => [:russian, :latin]
|
||||||
|
# end
|
||||||
|
# }
|
||||||
|
end
|
||||||
12
db/migrate/20210917074250_add_slug_to_district.rb
Normal file
12
db/migrate/20210917074250_add_slug_to_district.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
class AddSlugToDistrict < ActiveRecord::Migration[5.0]
|
||||||
|
def up
|
||||||
|
add_column :districts, :slug, :string
|
||||||
|
add_index :districts, :slug, unique: true
|
||||||
|
District.all.each {|district| district.update(slug: district.slug ||= district.name.parameterize) }
|
||||||
|
end
|
||||||
|
|
||||||
|
def down
|
||||||
|
remove_column :districts, :slug
|
||||||
|
end
|
||||||
|
|
||||||
|
end
|
||||||
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 20210916143538) do
|
ActiveRecord::Schema.define(version: 20210917074250) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
|
@ -62,6 +62,8 @@ ActiveRecord::Schema.define(version: 20210916143538) do
|
||||||
t.integer "state_id"
|
t.integer "state_id"
|
||||||
t.datetime "created_at", null: false
|
t.datetime "created_at", null: false
|
||||||
t.datetime "updated_at", null: false
|
t.datetime "updated_at", null: false
|
||||||
|
t.string "slug"
|
||||||
|
t.index ["slug"], name: "index_districts_on_slug", unique: true, using: :btree
|
||||||
end
|
end
|
||||||
|
|
||||||
create_table "question_lists", force: :cascade do |t|
|
create_table "question_lists", force: :cascade do |t|
|
||||||
|
|
|
||||||
|
|
@ -1,23 +1,5 @@
|
||||||
require 'rails_helper'
|
require 'rails_helper'
|
||||||
|
|
||||||
# This spec was generated by rspec-rails when you ran the scaffold generator.
|
|
||||||
# It demonstrates how one might use RSpec to specify the controller code that
|
|
||||||
# was generated by Rails when you ran the scaffold generator.
|
|
||||||
#
|
|
||||||
# It assumes that the implementation code is generated by the rails scaffold
|
|
||||||
# generator. If you are using any extension libraries to generate different
|
|
||||||
# controller code, this generated spec may or may not pass.
|
|
||||||
#
|
|
||||||
# It only uses APIs available in rails and/or rspec-rails. There are a number
|
|
||||||
# of tools you can use to make these specs even more expressive, but we're
|
|
||||||
# sticking to rails and rspec-rails APIs to keep things simple and stable.
|
|
||||||
#
|
|
||||||
# Compared to earlier versions of this generator, there is very limited use of
|
|
||||||
# stubs and message expectations in this spec. Stubs are only used when there
|
|
||||||
# is no simpler way to get a handle on the object needed for the example.
|
|
||||||
# Message expectations are only used when there is no simpler way to specify
|
|
||||||
# that an instance is receiving a specific message.
|
|
||||||
|
|
||||||
RSpec.describe DistrictsController, type: :controller do
|
RSpec.describe DistrictsController, type: :controller do
|
||||||
|
|
||||||
# This should return the minimal set of attributes required to create a valid
|
# This should return the minimal set of attributes required to create a valid
|
||||||
|
|
|
||||||
19
spec/models/district_spec.rb
Normal file
19
spec/models/district_spec.rb
Normal file
|
|
@ -0,0 +1,19 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe District, type: :model do
|
||||||
|
let(:district1) { District.create(name: 'District one', state_id: 32) }
|
||||||
|
let(:district2) { District.new(name: 'District two', state_id: 32) }
|
||||||
|
|
||||||
|
context "when saving or creating" do
|
||||||
|
it 'should return a slug' do
|
||||||
|
expect(district1.slug).to eq 'district-one'
|
||||||
|
|
||||||
|
district2.save
|
||||||
|
expect(district2.slug).to eq 'district-two'
|
||||||
|
|
||||||
|
first_district = District.find_by_slug('district-one')
|
||||||
|
expect(first_district.slug).to eq 'district-one'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue