active_model-permalink 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9ea1c85a8f0becc3a0d4523d919c8e197cd09051
4
+ data.tar.gz: fadbcae6d60cac5b60f317a68883acd3c6f56380
5
+ SHA512:
6
+ metadata.gz: a4381596a5df365bfb91dfe996b4f8408ba984cd9772c30bc5e9c64053283c4c5d284da637be50b497a18d57c688ca2adaf2a067c26a08d56763647c2a6849ba
7
+ data.tar.gz: b7738e93e83dcf7d3ebdb668f1b5ba31cd3b59d311652e50b435b7478a70f779621f60e754eedeea691d44702b9b4d95ae9567e1ae47071d66fed6c6aa1d09a9
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in active_model-permalink.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Attila Györffy
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.
@@ -0,0 +1,58 @@
1
+ # ActiveModel::Permalink
2
+
3
+ `ActiveModel::Permalink` generates permalinks for your `ActiveModel` objects. It includes support for `Mongoid`.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'active_model-permalink'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install active_model-permalink
20
+
21
+ ## Usage
22
+
23
+ `ActiveModel::Permalink` generates permalinks for your objects as part of a `before_validation` callback. It uses the following attributes in order to generate from (provided these are present):
24
+
25
+ * name
26
+ * title
27
+
28
+ In case the `permalink` attribute is present already, it won't bother to change it. See [the specs](https://github.com/liquid/active_model-permalink/blob/master/spec/lib/active_model/permalink_spec.rb) for more information about the behavior.
29
+
30
+ ### With simple ActiveModel classes
31
+
32
+ ```ruby
33
+ class MyClass
34
+ # Make sure you are using a real ActiveModel object
35
+ extend ActiveModel::Callbacks
36
+ include ActiveModel::Validations
37
+ include ActiveModel::Validations::Callbacks
38
+
39
+ include ActiveModel::Permalink
40
+
41
+ attr_accessor :name, :title, :permalink
42
+ end
43
+
44
+ my_instance = MyClass.new
45
+ my_instance.name = 'My Name'
46
+
47
+ my_instance.valid? # this triggers the callback that is used to ensure a permalink is present
48
+ my_instance.permalink
49
+ # => 'my-name'
50
+ ```
51
+
52
+ ## Contributing
53
+
54
+ 1. Fork it
55
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
56
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
57
+ 4. Push to the branch (`git push origin my-new-feature`)
58
+ 5. Create a new Pull Request
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'active_model/permalink/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "active_model-permalink"
8
+ spec.version = ActiveModel::Permalink::VERSION
9
+ spec.authors = ["Attila Györffy"]
10
+ spec.email = ["attila@attilagyorffy.com"]
11
+ spec.summary = %q{Simple permalink solution for ActiveModel objects}
12
+ spec.description = %q{ActiveModel::Permalink generates permalinks for your ActiveModel objects, including support for Mongoid.}
13
+ spec.homepage = "http://github.com/liquid/active_model-permalink"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_runtime_dependency "activemodel", [">= 4.0"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.7"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.1"
26
+ end
@@ -0,0 +1,33 @@
1
+ require 'active_support/concern'
2
+ require "active_model/permalink/version"
3
+
4
+ module ActiveModel
5
+ module Permalink
6
+
7
+ CANDIDATE_SOURCE_ATTRIBUTES = [:name, :title]
8
+
9
+ extend ActiveSupport::Concern
10
+
11
+ included do
12
+ before_validation :ensure_permalink_is_present
13
+ end
14
+
15
+ private
16
+
17
+ def ensure_permalink_is_present
18
+ self.permalink ||= generate_permalink
19
+ end
20
+
21
+ def generate_permalink
22
+ source_attribute = ActiveModel::Permalink::CANDIDATE_SOURCE_ATTRIBUTES.detect do |attr|
23
+ self.respond_to?(attr) && self.send(attr).present?
24
+ end
25
+
26
+ if source_attribute
27
+ self.send(source_attribute).dasherize.parameterize
28
+ else
29
+ ''
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,5 @@
1
+ module ActiveModel
2
+ module Permalink
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe ActiveModel::Permalink do
4
+ subject { TestClass.new }
5
+
6
+ context 'if permalink is not present' do
7
+ before do
8
+ subject.permalink = nil
9
+ end
10
+
11
+ context 'and :name is present' do
12
+ before do
13
+ subject.name = 'Name Of Model'
14
+ subject.title = 'Title Of Model'
15
+ subject.valid? # trigger callback
16
+ end
17
+
18
+ it 'generates from the :name attribute' do
19
+ expect(subject.permalink).to eql('name-of-model')
20
+ end
21
+ end
22
+
23
+ context 'and :name is not present but :title is' do
24
+ before do
25
+ subject.name = nil
26
+ subject.title = 'Title Of Model'
27
+ subject.valid? # trigger callback
28
+ end
29
+
30
+ it 'uses the :title attribute to generate the permalink from' do
31
+ expect(subject.permalink).to eql('title-of-model')
32
+ end
33
+ end
34
+ end
35
+
36
+ context 'if permalink is present already' do
37
+
38
+ before do
39
+ subject.permalink = 'already-existing'
40
+ subject.valid? # trigger the callback
41
+ end
42
+
43
+ it 'respects the value' do
44
+ subject.permalink = 'already-existing'
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,92 @@
1
+ require 'active_model/permalink'
2
+ require 'support/test_class'
3
+
4
+ # This file was generated by the `rspec --init` command. Conventionally, all
5
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
6
+ # The generated `.rspec` file contains `--require spec_helper` which will cause this
7
+ # file to always be loaded, without a need to explicitly require it in any files.
8
+ #
9
+ # Given that it is always loaded, you are encouraged to keep this file as
10
+ # light-weight as possible. Requiring heavyweight dependencies from this file
11
+ # will add to the boot time of your test suite on EVERY test run, even for an
12
+ # individual file that may not need all of that loaded. Instead, consider making
13
+ # a separate helper file that requires the additional dependencies and performs
14
+ # the additional setup, and require it from the spec files that actually need it.
15
+ #
16
+ # The `.rspec` file also contains a few flags that are not defaults but that
17
+ # users commonly want.
18
+ #
19
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
20
+ RSpec.configure do |config|
21
+ # rspec-expectations config goes here. You can use an alternate
22
+ # assertion/expectation library such as wrong or the stdlib/minitest
23
+ # assertions if you prefer.
24
+ config.expect_with :rspec do |expectations|
25
+ # This option will default to `true` in RSpec 4. It makes the `description`
26
+ # and `failure_message` of custom matchers include text for helper methods
27
+ # defined using `chain`, e.g.:
28
+ # be_bigger_than(2).and_smaller_than(4).description
29
+ # # => "be bigger than 2 and smaller than 4"
30
+ # ...rather than:
31
+ # # => "be bigger than 2"
32
+ expectations.include_chain_clauses_in_custom_matcher_descriptions = true
33
+ end
34
+
35
+ # rspec-mocks config goes here. You can use an alternate test double
36
+ # library (such as bogus or mocha) by changing the `mock_with` option here.
37
+ config.mock_with :rspec do |mocks|
38
+ # Prevents you from mocking or stubbing a method that does not exist on
39
+ # a real object. This is generally recommended, and will default to
40
+ # `true` in RSpec 4.
41
+ mocks.verify_partial_doubles = true
42
+ end
43
+
44
+ # The settings below are suggested to provide a good initial experience
45
+ # with RSpec, but feel free to customize to your heart's content.
46
+ =begin
47
+ # These two settings work together to allow you to limit a spec run
48
+ # to individual examples or groups you care about by tagging them with
49
+ # `:focus` metadata. When nothing is tagged with `:focus`, all examples
50
+ # get run.
51
+ config.filter_run :focus
52
+ config.run_all_when_everything_filtered = true
53
+
54
+ # Limits the available syntax to the non-monkey patched syntax that is recommended.
55
+ # For more details, see:
56
+ # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
57
+ # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
58
+ # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
59
+ config.disable_monkey_patching!
60
+
61
+ # This setting enables warnings. It's recommended, but in some cases may
62
+ # be too noisy due to issues in dependencies.
63
+ config.warnings = true
64
+
65
+ # Many RSpec users commonly either run the entire suite or an individual
66
+ # file, and it's useful to allow more verbose output when running an
67
+ # individual spec file.
68
+ if config.files_to_run.one?
69
+ # Use the documentation formatter for detailed output,
70
+ # unless a formatter has already been configured
71
+ # (e.g. via a command-line flag).
72
+ config.default_formatter = 'doc'
73
+ end
74
+
75
+ # Print the 10 slowest examples and example groups at the
76
+ # end of the spec run, to help surface which specs are running
77
+ # particularly slow.
78
+ config.profile_examples = 10
79
+
80
+ # Run specs in random order to surface order dependencies. If you find an
81
+ # order dependency and want to debug it, you can fix the order by providing
82
+ # the seed, which is printed after each run.
83
+ # --seed 1234
84
+ config.order = :random
85
+
86
+ # Seed global randomization in this process using the `--seed` CLI option.
87
+ # Setting this allows you to use `--seed` to deterministically reproduce
88
+ # test failures related to randomization by passing the same `--seed` value
89
+ # as the one that triggered the failure.
90
+ Kernel.srand config.seed
91
+ =end
92
+ end
@@ -0,0 +1,13 @@
1
+ require 'active_model'
2
+
3
+ class TestClass
4
+ extend ActiveModel::Callbacks
5
+ include ActiveModel::Validations
6
+ include ActiveModel::Validations::Callbacks
7
+
8
+ include ActiveModel::Permalink
9
+
10
+ attr_accessor :name
11
+ attr_accessor :title
12
+ attr_accessor :permalink
13
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: active_model-permalink
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Attila Györffy
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-10-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activemodel
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '4.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '4.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.7'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.7'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.1'
69
+ description: ActiveModel::Permalink generates permalinks for your ActiveModel objects,
70
+ including support for Mongoid.
71
+ email:
72
+ - attila@attilagyorffy.com
73
+ executables: []
74
+ extensions: []
75
+ extra_rdoc_files: []
76
+ files:
77
+ - ".gitignore"
78
+ - ".rspec"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - active_model-permalink.gemspec
84
+ - lib/active_model/permalink.rb
85
+ - lib/active_model/permalink/version.rb
86
+ - spec/lib/active_model/permalink_spec.rb
87
+ - spec/spec_helper.rb
88
+ - spec/support/test_class.rb
89
+ homepage: http://github.com/liquid/active_model-permalink
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.2.2
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Simple permalink solution for ActiveModel objects
113
+ test_files:
114
+ - spec/lib/active_model/permalink_spec.rb
115
+ - spec/spec_helper.rb
116
+ - spec/support/test_class.rb