Merge pull request #2434 from consul/slug_generation_logic
Only change budget slugs if its on draft phase
This commit is contained in:
@@ -180,6 +180,10 @@ class Budget < ActiveRecord::Base
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
def generate_slug?
|
||||
slug.nil? || drafting?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
|
||||
@@ -13,5 +13,11 @@ class Budget
|
||||
def single_heading_group?
|
||||
headings.count == 1
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_slug?
|
||||
slug.nil? || budget.drafting?
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -28,5 +28,11 @@ class Budget
|
||||
investments.empty?
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_slug?
|
||||
slug.nil? || budget.drafting?
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,7 +2,7 @@ module Sluggable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
before_validation :generate_slug
|
||||
before_validation :generate_slug, if: :generate_slug?
|
||||
end
|
||||
|
||||
def generate_slug
|
||||
|
||||
@@ -220,7 +220,7 @@ FactoryBot.define do
|
||||
end
|
||||
|
||||
factory :budget do
|
||||
sequence(:name) { |n| "Budget #{n}" }
|
||||
sequence(:name) { |n| "#{Faker::Lorem.word} #{n}" }
|
||||
currency_symbol "€"
|
||||
phase 'accepting'
|
||||
description_drafting "This budget is drafting"
|
||||
@@ -278,6 +278,10 @@ FactoryBot.define do
|
||||
factory :budget_group, class: 'Budget::Group' do
|
||||
budget
|
||||
sequence(:name) { |n| "Group #{n}" }
|
||||
|
||||
trait :drafting_budget do
|
||||
association :budget, factory: [:budget, :drafting]
|
||||
end
|
||||
end
|
||||
|
||||
factory :budget_heading, class: 'Budget::Heading' do
|
||||
@@ -285,6 +289,10 @@ FactoryBot.define do
|
||||
sequence(:name) { |n| "Heading #{n}" }
|
||||
price 1000000
|
||||
population 1234
|
||||
|
||||
trait :drafting_budget do
|
||||
association :group, factory: [:budget_group, :drafting_budget]
|
||||
end
|
||||
end
|
||||
|
||||
factory :budget_investment, class: 'Budget::Investment' do
|
||||
|
||||
@@ -4,7 +4,7 @@ describe Budget::Group do
|
||||
|
||||
let(:budget) { create(:budget) }
|
||||
|
||||
it_behaves_like "sluggable"
|
||||
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
|
||||
|
||||
describe "name" do
|
||||
before do
|
||||
|
||||
@@ -5,7 +5,7 @@ describe Budget::Heading do
|
||||
let(:budget) { create(:budget) }
|
||||
let(:group) { create(:budget_group, budget: budget) }
|
||||
|
||||
it_behaves_like "sluggable"
|
||||
it_behaves_like "sluggable", updatable_slug_trait: :drafting_budget
|
||||
|
||||
describe "name" do
|
||||
before do
|
||||
|
||||
@@ -4,7 +4,7 @@ describe Budget do
|
||||
|
||||
let(:budget) { create(:budget) }
|
||||
|
||||
it_behaves_like "sluggable"
|
||||
it_behaves_like "sluggable", updatable_slug_trait: :drafting
|
||||
|
||||
describe "name" do
|
||||
before do
|
||||
|
||||
@@ -1,15 +1,29 @@
|
||||
require 'spec_helper'
|
||||
|
||||
shared_examples_for 'sluggable' do
|
||||
shared_examples_for 'sluggable' do |updatable_slug_trait:|
|
||||
|
||||
describe 'generate_slug' do
|
||||
before do
|
||||
create(described_class.name.parameterize.tr('-', '_').to_sym, name: "Marlo Brañido Carlo")
|
||||
end
|
||||
let(:factory_name) { described_class.name.parameterize('_').to_sym }
|
||||
let(:sluggable) { create(factory_name, name: "Marló Brañido Carlo") }
|
||||
|
||||
context "when a new sluggable is created" do
|
||||
it "gets a slug string" do
|
||||
expect(described_class.last.slug).to eq("marlo-branido-carlo")
|
||||
expect(sluggable.slug).to eq("marlo-branido-carlo")
|
||||
end
|
||||
end
|
||||
|
||||
context "slug updating condition is true" do
|
||||
it "slug is updated" do
|
||||
updatable = create(factory_name, updatable_slug_trait, name: 'Old Name')
|
||||
expect{updatable.update_attributes(name: 'New Name')}
|
||||
.to change{ updatable.slug }.from('old-name').to('new-name')
|
||||
end
|
||||
end
|
||||
|
||||
context "slug updating condition is false" do
|
||||
it "slug isn't updated" do
|
||||
expect{sluggable.update_attributes(name: 'New Name')}
|
||||
.not_to (change{ sluggable.slug })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user