|
| 1 | +require "../spec_helper" |
| 2 | + |
| 3 | +describe Avram::Slugify do |
| 4 | + describe ".set" do |
| 5 | + it "does not set anything if slug is already set" do |
| 6 | + op = build_op(title: "Writing Specs") |
| 7 | + |
| 8 | + slugify(op.slug, "Writing Specs") |
| 9 | + |
| 10 | + op.slug.value.should eq("writing-specs") |
| 11 | + end |
| 12 | + |
| 13 | + it "skips blank slug candidates" do |
| 14 | + op = build_op(title: "Software Developer") |
| 15 | + |
| 16 | + slugify(op.slug, ["", op.title]) |
| 17 | + |
| 18 | + op.slug.value.should eq("software-developer") |
| 19 | + end |
| 20 | + |
| 21 | + describe "with a single slug candidate" do |
| 22 | + it "it sets slug from a single attribute" do |
| 23 | + op = build_op(title: "Software Developer") |
| 24 | + |
| 25 | + slugify(op.slug, op.title) |
| 26 | + |
| 27 | + op.slug.value.should eq("software-developer") |
| 28 | + end |
| 29 | + |
| 30 | + it "it sets slug from a single string" do |
| 31 | + op = build_op |
| 32 | + |
| 33 | + slugify(op.slug, "Software Developer") |
| 34 | + |
| 35 | + op.slug.value.should eq("software-developer") |
| 36 | + end |
| 37 | + |
| 38 | + it "it sets slug from a single string" do |
| 39 | + op = build_op |
| 40 | + |
| 41 | + slugify(op.slug, "Software Developer") |
| 42 | + |
| 43 | + op.slug.value.should eq("software-developer") |
| 44 | + end |
| 45 | + end |
| 46 | + |
| 47 | + describe "with an array of slug candidates" do |
| 48 | + describe "and there is no other record with the same slug" do |
| 49 | + it "sets using a String" do |
| 50 | + op = build_op |
| 51 | + |
| 52 | + slugify(op.slug, ["Software Developer"]) |
| 53 | + |
| 54 | + op.slug.value.should eq("software-developer") |
| 55 | + end |
| 56 | + |
| 57 | + it "sets using an attribute" do |
| 58 | + op = build_op(title: "Software Developer") |
| 59 | + |
| 60 | + slugify(op.slug, [op.title]) |
| 61 | + |
| 62 | + op.slug.value.should eq("software-developer") |
| 63 | + end |
| 64 | + |
| 65 | + it "sets when using multiple attributes" do |
| 66 | + op = build_op(title: "How Do Magnets Work?", sub_heading: "And Why?") |
| 67 | + |
| 68 | + slugify(op.slug, [[op.title, op.sub_heading]]) |
| 69 | + |
| 70 | + op.slug.value.should eq("how-do-magnets-work-and-why") |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + describe "and the first slug candidate is not unique" do |
| 75 | + it "chooses the first unique one in the array" do |
| 76 | + ArticleFactory.create &.slug("music") |
| 77 | + ArticleFactory.create &.slug("programming") |
| 78 | + op = build_op(title: "Music", sub_heading: "Programming") |
| 79 | + |
| 80 | + slugify(op.slug, [op.title, "programming", [op.title, op.sub_heading]]) |
| 81 | + |
| 82 | + op.slug.value.should eq("music-programming") |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + describe "and all of the slug candidates are used already" do |
| 87 | + it "uses the first present candidate and appends a UUID" do |
| 88 | + ArticleFactory.create &.slug("pizza") |
| 89 | + ArticleFactory.create &.slug("tacos") |
| 90 | + op = build_op(title: "Pizza", sub_heading: "Tacos") |
| 91 | + |
| 92 | + # First string is empty. Added to make sure it is not used with |
| 93 | + # the UUID. |
| 94 | + slugify(op.slug, ["", op.title, op.sub_heading]) |
| 95 | + |
| 96 | + op.slug.value.to_s.should start_with("pizza-") |
| 97 | + op.slug.value.to_s.split("-", 2).last.size.should eq(UUID.random.to_s.size) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + describe "all slug candidates are blank" do |
| 102 | + it "leaves the slug as nil" do |
| 103 | + op = build_op(title: "") |
| 104 | + |
| 105 | + # First string is empty. Added to make sure it is not used with |
| 106 | + # the UUID. |
| 107 | + slugify(op.slug, ["", op.title]) |
| 108 | + |
| 109 | + op.slug.value.should be_nil |
| 110 | + end |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + it "uses the query to scope uniqueness check" do |
| 115 | + ArticleFactory.create &.slug("the-boss").title("A") |
| 116 | + |
| 117 | + op = build_op(title: "The Boss") |
| 118 | + slugify(op.slug, op.title, ArticleQuery.new.title("B")) |
| 119 | + op.slug.value.should eq("the-boss") |
| 120 | + |
| 121 | + op = build_op(title: "The Boss") |
| 122 | + slugify(op.slug, op.title, ArticleQuery.new.title("A")) |
| 123 | + op.slug.value.to_s.should start_with("the-boss-") # Has UUID appended |
| 124 | + end |
| 125 | + end |
| 126 | +end |
| 127 | + |
| 128 | +private def slugify(slug, slug_candidates, query = ArticleQuery.new) |
| 129 | + Avram::Slugify.set(slug, slug_candidates, query) |
| 130 | +end |
| 131 | + |
| 132 | +private def build_op(**named_args) |
| 133 | + Article::SaveOperation.new(**named_args) |
| 134 | +end |
0 commit comments