Skip to content

Commit

Permalink
feat(organizations): adds model (nsarno#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
muZk committed Jul 8, 2020
1 parent de45e29 commit d1779fe
Show file tree
Hide file tree
Showing 11 changed files with 198 additions and 1 deletion.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.7.1'

gem 'friendly_id', '~> 5.2.4'

# Random data
gem 'faker'

Expand Down
3 changes: 3 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ GEM
faker (2.11.0)
i18n (>= 1.6, < 2)
ffi (1.12.2)
friendly_id (5.2.5)
activerecord (>= 4.0.0)
globalid (0.4.2)
activesupport (>= 4.2.0)
i18n (1.8.2)
Expand Down Expand Up @@ -251,6 +253,7 @@ DEPENDENCIES
devise
factory_bot_rails
faker
friendly_id (~> 5.2.4)
jbuilder (~> 2.7)
knock!
listen (>= 3.0.5, < 3.2)
Expand Down
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: bin/rails server -p ${PORT:-5000} -e $RAILS_ENV
release: bundle exec rake db:migrate
8 changes: 8 additions & 0 deletions app/models/organization.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class Organization < ApplicationRecord
validates_presence_of :name

extend FriendlyId
friendly_id :name, use: :slugged
end
107 changes: 107 additions & 0 deletions config/initializers/friendly_id.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
# 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)

# This adds an option to treat reserved words as conflicts rather than exceptions.
# When there is no good candidate, a UUID will be appended, matching the existing
# conflict behavior.

# config.treat_reserved_as_conflict = true

# ## 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'
#
# By default, slug has no size limit, but you can change it if you wish.
#
# config.slug_limit = 255
#
# 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 = '-'
#
# Note that you must use the :slugged addon **prior** to the line which
# configures the sequence separator, or else FriendlyId will raise an undefined
# method error.
#
# ## 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.
# Note: Use(include) Slugged module in the config if using the anonymous module.
# If you have `friendly_id :name, use: slugged` in the model, Slugged module
# is included after the anonymous module defined in the initializer, so it
# overrides the `should_generate_new_friendly_id?` method from the anonymous module.
#
# config.use :slugged
# 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
9 changes: 9 additions & 0 deletions db/migrate/20200708212711_create_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateOrganizations < ActiveRecord::Migration[6.0]
def change
create_table :organizations do |t|
t.string :name

t.timestamps
end
end
end
6 changes: 6 additions & 0 deletions db/migrate/20200708212804_add_slug_to_organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddSlugToOrganizations < ActiveRecord::Migration[6.0]
def change
add_column :organizations, :slug, :string
add_index :organizations, :slug, unique: true
end
end
21 changes: 21 additions & 0 deletions db/migrate/20200708212824_create_friendly_id_slugs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIGRATION_CLASS =
if ActiveRecord::VERSION::MAJOR >= 5
ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}"]
else
ActiveRecord::Migration
end

class CreateFriendlyIdSlugs < MIGRATION_CLASS
def change
create_table :friendly_id_slugs do |t|
t.string :slug, :null => false
t.integer :sluggable_id, :null => false
t.string :sluggable_type, :limit => 50
t.string :scope
t.datetime :created_at
end
add_index :friendly_id_slugs, [:sluggable_type, :sluggable_id]
add_index :friendly_id_slugs, [:slug, :sluggable_type], length: { slug: 140, sluggable_type: 50 }
add_index :friendly_id_slugs, [:slug, :sluggable_type, :scope], length: { slug: 70, sluggable_type: 50, scope: 70 }, unique: true
end
end
21 changes: 20 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_07_01_230933) do
ActiveRecord::Schema.define(version: 2020_07_08_212824) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -36,6 +36,17 @@
t.index ["user_id"], name: "index_calendar_rules_on_user_id"
end

create_table "friendly_id_slugs", force: :cascade do |t|
t.string "slug", null: false
t.integer "sluggable_id", null: false
t.string "sluggable_type", limit: 50
t.string "scope"
t.datetime "created_at"
t.index ["slug", "sluggable_type", "scope"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type_and_scope", unique: true
t.index ["slug", "sluggable_type"], name: "index_friendly_id_slugs_on_slug_and_sluggable_type"
t.index ["sluggable_type", "sluggable_id"], name: "index_friendly_id_slugs_on_sluggable_type_and_sluggable_id"
end

create_table "lessons", force: :cascade do |t|
t.text "description"
t.string "topic_name"
Expand All @@ -49,6 +60,14 @@
t.index ["tutor_id"], name: "index_lessons_on_tutor_id"
end

create_table "organizations", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "slug"
t.index ["slug"], name: "index_organizations_on_slug", unique: true
end

create_table "topics", force: :cascade do |t|
t.string "name"
t.datetime "created_at", precision: 6, null: false
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/organizations.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

FactoryBot.define do
factory :organization do
name { 'PreuGauss' }
end
end
13 changes: 13 additions & 0 deletions spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

require 'rails_helper'

RSpec.describe Organization, type: :model do
subject { create :organization }

describe 'validations' do
describe 'required fields' do
it { should validate_presence_of :name }
end
end
end

0 comments on commit d1779fe

Please sign in to comment.