From 895b95e885beeaaca8b452bcc28c11d2c451132d Mon Sep 17 00:00:00 2001 From: John Bintz Date: Fri, 14 Dec 2012 17:40:17 -0500 Subject: [PATCH] initial commit --- .gitignore | 17 ++++++ Gemfile | 4 ++ LICENSE.txt | 22 ++++++++ README.md | 13 +++++ Rakefile | 1 + formtastic-slug.gemspec | 19 +++++++ lib/formtastic-slug.rb | 3 + lib/formtastic-slug/railtie.rb | 12 ++++ lib/formtastic-slug/slug_input.rb | 93 +++++++++++++++++++++++++++++++ lib/formtastic-slug/version.rb | 5 ++ 10 files changed, 189 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE.txt create mode 100644 README.md create mode 100644 Rakefile create mode 100644 formtastic-slug.gemspec create mode 100644 lib/formtastic-slug.rb create mode 100644 lib/formtastic-slug/railtie.rb create mode 100644 lib/formtastic-slug/slug_input.rb create mode 100644 lib/formtastic-slug/version.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -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 diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..5f926d3 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in formtastic-slug.gemspec +gemspec diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..87b491e --- /dev/null +++ b/LICENSE.txt @@ -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. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..c3f4215 --- /dev/null +++ b/README.md @@ -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' +``` diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..2995527 --- /dev/null +++ b/Rakefile @@ -0,0 +1 @@ +require "bundler/gem_tasks" diff --git a/formtastic-slug.gemspec b/formtastic-slug.gemspec new file mode 100644 index 0000000..7b5e6db --- /dev/null +++ b/formtastic-slug.gemspec @@ -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 diff --git a/lib/formtastic-slug.rb b/lib/formtastic-slug.rb new file mode 100644 index 0000000..3ebd2e2 --- /dev/null +++ b/lib/formtastic-slug.rb @@ -0,0 +1,3 @@ +require "formtastic-slug/version" +require "formtastic-slug/railtie" if defined?(Rails::Railtie) + diff --git a/lib/formtastic-slug/railtie.rb b/lib/formtastic-slug/railtie.rb new file mode 100644 index 0000000..17998b0 --- /dev/null +++ b/lib/formtastic-slug/railtie.rb @@ -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 + diff --git a/lib/formtastic-slug/slug_input.rb b/lib/formtastic-slug/slug_input.rb new file mode 100644 index 0000000..9f72cb2 --- /dev/null +++ b/lib/formtastic-slug/slug_input.rb @@ -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 + + JS + end +end + diff --git a/lib/formtastic-slug/version.rb b/lib/formtastic-slug/version.rb new file mode 100644 index 0000000..0891e5e --- /dev/null +++ b/lib/formtastic-slug/version.rb @@ -0,0 +1,5 @@ +module Formtastic + module Slug + VERSION = "0.0.1" + end +end