initial commit
This commit is contained in:
commit
895b95e885
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
*.gem
|
||||||
|
*.rbc
|
||||||
|
.bundle
|
||||||
|
.config
|
||||||
|
.yardoc
|
||||||
|
Gemfile.lock
|
||||||
|
InstalledFiles
|
||||||
|
_yardoc
|
||||||
|
coverage
|
||||||
|
doc/
|
||||||
|
lib/bundler/man
|
||||||
|
pkg
|
||||||
|
rdoc
|
||||||
|
spec/reports
|
||||||
|
test/tmp
|
||||||
|
test/version_tmp
|
||||||
|
tmp
|
4
Gemfile
Normal file
4
Gemfile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
source 'https://rubygems.org'
|
||||||
|
|
||||||
|
# Specify your gem's dependencies in formtastic-slug.gemspec
|
||||||
|
gemspec
|
22
LICENSE.txt
Normal file
22
LICENSE.txt
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
Copyright (c) 2012 John Bintz
|
||||||
|
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining
|
||||||
|
a copy of this software and associated documentation files (the
|
||||||
|
"Software"), to deal in the Software without restriction, including
|
||||||
|
without limitation the rights to use, copy, modify, merge, publish,
|
||||||
|
distribute, sublicense, and/or sell copies of the Software, and to
|
||||||
|
permit persons to whom the Software is furnished to do so, subject to
|
||||||
|
the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be
|
||||||
|
included in all copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||||
|
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||||
|
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||||
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||||
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||||
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||||
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
13
README.md
Normal file
13
README.md
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
Add a slug field, that generates a slug based on another field.
|
||||||
|
|
||||||
|
``` haml
|
||||||
|
= semantic_form_for @blog_entry do |f|
|
||||||
|
= f.input :title
|
||||||
|
= f.input :slug, :as => :slug, :based_on => :title
|
||||||
|
```
|
||||||
|
|
||||||
|
Just add it to your Gemfile:
|
||||||
|
|
||||||
|
``` ruby
|
||||||
|
gem 'formtastic-slug', :git => 'git://github.com/johnbintz/formtastic-slug.git'
|
||||||
|
```
|
19
formtastic-slug.gemspec
Normal file
19
formtastic-slug.gemspec
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
# -*- encoding: utf-8 -*-
|
||||||
|
lib = File.expand_path('../lib', __FILE__)
|
||||||
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
||||||
|
require 'formtastic-slug/version'
|
||||||
|
|
||||||
|
Gem::Specification.new do |gem|
|
||||||
|
gem.name = "formtastic-slug"
|
||||||
|
gem.version = Formtastic::Slug::VERSION
|
||||||
|
gem.authors = ["John Bintz"]
|
||||||
|
gem.email = ["john@coswellproductions.com"]
|
||||||
|
gem.description = %q{Add a slug field that can be constructed from another field}
|
||||||
|
gem.summary = %q{Add a slug field that can be constructed from another field}
|
||||||
|
gem.homepage = ""
|
||||||
|
|
||||||
|
gem.files = `git ls-files`.split($/)
|
||||||
|
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
||||||
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
||||||
|
gem.require_paths = ["lib"]
|
||||||
|
end
|
3
lib/formtastic-slug.rb
Normal file
3
lib/formtastic-slug.rb
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
require "formtastic-slug/version"
|
||||||
|
require "formtastic-slug/railtie" if defined?(Rails::Railtie)
|
||||||
|
|
12
lib/formtastic-slug/railtie.rb
Normal file
12
lib/formtastic-slug/railtie.rb
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
module Formtastic
|
||||||
|
module Slug
|
||||||
|
class Railtie < ::Rails::Railtie
|
||||||
|
initializer 'formtastic-slug.initialize' do
|
||||||
|
if defined?(Formtastic::Inputs)
|
||||||
|
require 'formtastic-slug/slug_input'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
93
lib/formtastic-slug/slug_input.rb
Normal file
93
lib/formtastic-slug/slug_input.rb
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
class SlugInput < Formtastic::Inputs::StringInput
|
||||||
|
def to_html
|
||||||
|
input_wrapping do
|
||||||
|
label_html <<
|
||||||
|
action_buttons <<
|
||||||
|
builder.text_field(method, input_html_options.merge(:disabled => true)) <<
|
||||||
|
slug_js_setup
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def action_buttons
|
||||||
|
template.button_tag('Edit', 'data-mode' => 'disabled', 'data-action' => 'edit') <<
|
||||||
|
template.button_tag('Update', 'data-mode' => 'enabled', 'data-action' => 'update', :style => 'display: none') <<
|
||||||
|
template.button_tag('Cancel', 'data-mode' => 'enabled', 'data-action' => 'cancel', :style => 'display: none')
|
||||||
|
end
|
||||||
|
|
||||||
|
def input_html_options
|
||||||
|
super.merge(:style => 'width: 40%')
|
||||||
|
end
|
||||||
|
|
||||||
|
def target_dom_id
|
||||||
|
[
|
||||||
|
builder.custom_namespace,
|
||||||
|
sanitized_object_name,
|
||||||
|
target_method_name
|
||||||
|
].reject { |x| x.blank? }.join('_')
|
||||||
|
end
|
||||||
|
|
||||||
|
def target_method_name
|
||||||
|
@target_method_name ||= options.delete(:based_on)
|
||||||
|
end
|
||||||
|
|
||||||
|
def sanitized_target_method_name
|
||||||
|
target_method_name.to_s.gsub(/[\?\/\-]$/, '')
|
||||||
|
end
|
||||||
|
|
||||||
|
def slug_js_setup
|
||||||
|
(<<-JS).html_safe
|
||||||
|
<script type="text/javascript">
|
||||||
|
(function() {
|
||||||
|
var targetField = $('##{target_dom_id}');
|
||||||
|
var slugField = $('##{dom_id}');
|
||||||
|
var slugInputWrap = $('##{dom_id}_input');
|
||||||
|
|
||||||
|
var setSlug = function(text) {
|
||||||
|
slugField.val(text.toLowerCase().replace(/[^a-z0-9]+/g, '-'));
|
||||||
|
};
|
||||||
|
|
||||||
|
targetField.on('blur change', function(e) {
|
||||||
|
if (slugField.val() == '') {
|
||||||
|
setSlug(targetField.val());
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
|
slugInputWrap.find('[data-action=edit]').on('click', function(e) {
|
||||||
|
slugInputWrap.find('button').toggle();
|
||||||
|
slugField.attr('disabled', false);
|
||||||
|
slugField.data('original', slugField.val());
|
||||||
|
slugField.focus();
|
||||||
|
return false;
|
||||||
|
});
|
||||||
|
|
||||||
|
var resetToDisabled = function() {
|
||||||
|
slugInputWrap.find('button').toggle();
|
||||||
|
slugField.attr('disabled', true);
|
||||||
|
return false;
|
||||||
|
};
|
||||||
|
|
||||||
|
slugInputWrap.find('[data-action=cancel]').on('click', function(e) {
|
||||||
|
slugField.val(slugField.data('original'));
|
||||||
|
return resetToDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
slugInputWrap.find('[data-action=update]').on('click', function(e) {
|
||||||
|
setSlug(slugField.val());
|
||||||
|
return resetToDisabled();
|
||||||
|
});
|
||||||
|
|
||||||
|
slugField.closest('form').on('submit', function() {
|
||||||
|
slugField.attr('disabled', false);
|
||||||
|
if (slugField.val() == '') {
|
||||||
|
setSlug(targetField.val());
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
})(this);
|
||||||
|
</script>
|
||||||
|
JS
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
5
lib/formtastic-slug/version.rb
Normal file
5
lib/formtastic-slug/version.rb
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
module Formtastic
|
||||||
|
module Slug
|
||||||
|
VERSION = "0.0.1"
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user