slugworth 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 04ab52605f883c0def4b390bcc679b8ca3e02e1b
4
+ data.tar.gz: 015b1c107147250b77fb16e89c5cb21577982c29
5
+ SHA512:
6
+ metadata.gz: cf4eafd11571e1243cc29e9e40b9589fc9cebfa435785ca2f9523dbf0b7eb5864b9d7cdf11c57cc60d23937fe0187ba0b1edc9f703ec0c0c0124603575b5b527
7
+ data.tar.gz: 595902d54c36d528dc60a9a47544eccd56ab0fc990da6d56044ce3c3f71153c0f52c3a94d4dd3fb7e9a7904d3f278a461f2edef8b62901f241e1ec33e863a858
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ slugworth
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slugworth.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Matt Polito
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,56 @@
1
+ # Slugworth
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/slugworth.png)](http://badge.fury.io/rb/sluggle)
4
+ [![Build Status](https://travis-ci.org/mattpolito/slugworth.png?branch=master)](https://travis-ci.org/mattpolito/sluggle)
5
+ [![Code Climate](https://codeclimate.com/github/mattpolito/slugworth.png)](https://codeclimate.com/github/mattpolito/sluggle)
6
+
7
+ Simple slug functionality for your ActiveRecord objects
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```shell
14
+ gem 'slugworth'
15
+ ```
16
+
17
+ and then execute
18
+
19
+ ```shell
20
+ bundle
21
+ ```
22
+
23
+ or
24
+
25
+ install it yourself with
26
+
27
+ ```shell
28
+ gem install slugworth
29
+ ```
30
+
31
+ ## Usage
32
+
33
+ Getting started is easy! Just ensure that your model has a database column of type `String` called `slug`
34
+
35
+ To use just add two declarations to your module and away you go!
36
+
37
+ ```ruby
38
+ class User < ActiveRecord::Base
39
+ include Slugworth
40
+ slugged_with :name
41
+ end
42
+ ```
43
+
44
+ This provides most of the default slug functionality you would need.
45
+
46
+ * A finder `.find_by_slug` is provided. This will, of course, do what you expect and find a single record by the slug attribute provided. However, it will throw an `ActiveRecord::RecordNotFound` exception... if not found.
47
+ * `#to_param` has been defined as a paramaterized version of the attribute declared to `slugged_with`.
48
+ * Validations stating that slug is present and unique in the database.
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/slugworth.rb ADDED
@@ -0,0 +1,32 @@
1
+ require "slugworth/version"
2
+
3
+ module Slugworth
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ cattr_accessor :slug_attribute
8
+ before_validation(:add_slug)
9
+ validates_uniqueness_of :slug
10
+ end
11
+
12
+ module ClassMethods
13
+ def slugged_with(slug_attribute)
14
+ self.slug_attribute = slug_attribute
15
+ end
16
+
17
+ def find_by_slug(slug)
18
+ find_by!(slug: slug)
19
+ end
20
+ end
21
+
22
+ def to_param; slug; end
23
+
24
+ private
25
+ def add_slug
26
+ self.slug = processed_slug unless slug.present?
27
+ end
28
+
29
+ def processed_slug
30
+ send(slug_attribute).parameterize
31
+ end
32
+ end
@@ -0,0 +1,3 @@
1
+ module Slugworth
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,58 @@
1
+ shared_examples_for :has_slug_functionality do
2
+ describe "#slug" do
3
+ context "when no slug is set" do
4
+ specify 'slug is generated' do
5
+ obj = described_class.new(described_class.slug_attribute => 'Generated slug')
6
+ obj.valid?
7
+ expect(obj.slug).to eq('generated-slug')
8
+ end
9
+ end
10
+
11
+ context "when the slug is set" do
12
+ specify 'slug is set to passed value' do
13
+ obj = described_class.new(slug: 'passed-slug')
14
+ obj.valid?
15
+ expect(obj.slug).to eq('passed-slug')
16
+ end
17
+ end
18
+
19
+ context "when slug is already taken" do
20
+ before do
21
+ existing = described_class.new(slug: 'taken-slug')
22
+ existing.save(validate: false)
23
+ end
24
+
25
+ specify "object is not valid" do
26
+ obj = described_class.new(slug: 'taken-slug')
27
+ obj.valid?
28
+ expect(obj.errors[:slug]).to_not be_empty
29
+ end
30
+ end
31
+ end
32
+
33
+ describe '#to_param' do
34
+ specify 'parameter uses slug' do
35
+ obj = described_class.new(slug: 'passed-slug')
36
+ expect(obj.to_param).to eq('passed-slug')
37
+ end
38
+ end
39
+
40
+ describe ".find_by_slug" do
41
+ context 'when record is available to be found' do
42
+ before do
43
+ User.create(slug: 'named-slug')
44
+ end
45
+
46
+ specify 'record is returned' do
47
+ record = described_class.find_by_slug('named-slug')
48
+ expect(record).to be_present
49
+ end
50
+ end
51
+
52
+ context 'when record is not found' do
53
+ specify 'error is returned' do
54
+ expect { described_class.find_by_slug('named-slug') }.to raise_error(ActiveRecord::RecordNotFound)
55
+ end
56
+ end
57
+ end
58
+ end
data/slugworth.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slugworth/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slugworth"
8
+ spec.version = Slugworth::VERSION
9
+ spec.authors = ["Matt Polito"]
10
+ spec.email = ["matt.polito@gmail.com"]
11
+ spec.description = %q{Easy slug functionality}
12
+ spec.summary = %q{Easy slug functionality}
13
+ spec.homepage = "https://github.com/mattpolito/slugworth"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec", "~> 2.13"
24
+ spec.add_development_dependency 'activerecord', '~> 4.0.0'
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "database_cleaner"
27
+ spec.add_development_dependency "sqlite3"
28
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+ require 'slugworth_shared_examples'
3
+ require 'slugworth'
4
+
5
+ ActiveRecord::Base.connection.execute(
6
+ %{CREATE TABLE users (id INTEGER PRIMARY KEY, name STRING, slug STRING);}
7
+ )
8
+
9
+ class User < ActiveRecord::Base
10
+ include Slugworth
11
+ slugged_with :name
12
+ end
13
+
14
+ describe User do
15
+ it_behaves_like :has_slug_functionality
16
+ end
@@ -0,0 +1,26 @@
1
+ require 'rspec'
2
+ require 'database_cleaner'
3
+
4
+ require 'active_support/concern'
5
+ require 'active_record'
6
+
7
+ ActiveRecord::Base.establish_connection(
8
+ adapter: 'sqlite3', database: ':memory:'
9
+ )
10
+
11
+ RSpec.configure do |config|
12
+ config.order = "random"
13
+
14
+ config.before(:suite) do
15
+ DatabaseCleaner.clean_with(:truncation)
16
+ end
17
+
18
+ config.before(:each) do
19
+ DatabaseCleaner.strategy = :transaction
20
+ DatabaseCleaner.start
21
+ end
22
+
23
+ config.after(:each) do
24
+ DatabaseCleaner.clean
25
+ end
26
+ end
metadata ADDED
@@ -0,0 +1,157 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slugworth
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Matt Polito
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '2.13'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '2.13'
55
+ - !ruby/object:Gem::Dependency
56
+ name: activerecord
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: 4.0.0
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: 4.0.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: database_cleaner
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: sqlite3
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Easy slug functionality
112
+ email:
113
+ - matt.polito@gmail.com
114
+ executables: []
115
+ extensions: []
116
+ extra_rdoc_files: []
117
+ files:
118
+ - .gitignore
119
+ - .ruby-gemset
120
+ - .ruby-version
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - lib/slugworth.rb
126
+ - lib/slugworth/version.rb
127
+ - lib/slugworth_shared_examples.rb
128
+ - slugworth.gemspec
129
+ - spec/sluggle_spec.rb
130
+ - spec/spec_helper.rb
131
+ homepage: https://github.com/mattpolito/slugworth
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - '>='
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - '>='
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.0.3
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Easy slug functionality
155
+ test_files:
156
+ - spec/sluggle_spec.rb
157
+ - spec/spec_helper.rb