From 5cda866980299e83455234682b1779e9270f4877 Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 17 Oct 2012 14:40:13 -0400 Subject: [PATCH] initial commit --- .gitignore | 17 ++++++++ Gemfile | 4 ++ LICENSE | 22 ++++++++++ README.md | 9 ++++ Rakefile | 2 + formtastic-better_file_inputs.gemspec | 17 ++++++++ lib/formtastic-better_file_inputs.rb | 3 ++ .../file_field_input.rb | 42 +++++++++++++++++++ .../image_field_input.rb | 6 +++ lib/formtastic-better_file_inputs/railtie.rb | 13 ++++++ lib/formtastic-better_file_inputs/version.rb | 5 +++ 11 files changed, 140 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Rakefile create mode 100644 formtastic-better_file_inputs.gemspec create mode 100644 lib/formtastic-better_file_inputs.rb create mode 100644 lib/formtastic-better_file_inputs/file_field_input.rb create mode 100644 lib/formtastic-better_file_inputs/image_field_input.rb create mode 100644 lib/formtastic-better_file_inputs/railtie.rb create mode 100644 lib/formtastic-better_file_inputs/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..e083a68 --- /dev/null +++ b/Gemfile @@ -0,0 +1,4 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in formtastic-better_file_inputs.gemspec +gemspec diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..87b491e --- /dev/null +++ b/LICENSE @@ -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..3ff7e1b --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +Built for Active Admin primarily. You get the following: + +``` ruby += f.input :file, :as => :file_field += f.input :image, :as => :image_field +``` + +You can remove the existing file, and get a preview of uploaded images. Will add more as I go. + diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..f57ae68 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +#!/usr/bin/env rake +require "bundler/gem_tasks" diff --git a/formtastic-better_file_inputs.gemspec b/formtastic-better_file_inputs.gemspec new file mode 100644 index 0000000..d3debb5 --- /dev/null +++ b/formtastic-better_file_inputs.gemspec @@ -0,0 +1,17 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../lib/formtastic-better_file_inputs/version', __FILE__) + +Gem::Specification.new do |gem| + gem.authors = ["John Bintz"] + gem.email = ["john@coswellproductions.com"] + gem.description = %q{TODO: Write a gem description} + gem.summary = %q{TODO: Write a gem summary} + 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.name = "formtastic-better_file_inputs" + gem.require_paths = ["lib"] + gem.version = Formtastic::BetterFileInputs::VERSION +end diff --git a/lib/formtastic-better_file_inputs.rb b/lib/formtastic-better_file_inputs.rb new file mode 100644 index 0000000..094e7b2 --- /dev/null +++ b/lib/formtastic-better_file_inputs.rb @@ -0,0 +1,3 @@ +require "formtastic-better_file_inputs/version" +require "formtastic-better_file_inputs/railtie" if defined?(Rails::Railtie) + diff --git a/lib/formtastic-better_file_inputs/file_field_input.rb b/lib/formtastic-better_file_inputs/file_field_input.rb new file mode 100644 index 0000000..8954bc1 --- /dev/null +++ b/lib/formtastic-better_file_inputs/file_field_input.rb @@ -0,0 +1,42 @@ +require 'delegate' + +class FileFieldInput < Formtastic::Inputs::FileInput + def file_field + builder.file_field(method, input_html_options) + end + + def remove_checkbox + template.content_tag(:label) do + builder.check_box("remove_#{method}") << localized_string(method, " Remove", :"remove_#{method}") + end + end + + def preview + '' + end + + def url_display + template.content_tag(:div, @url) << template.content_tag(:br) + end + + def existing_file_info + if @url = builder.object.send(method).url + preview << remove_checkbox << url_display + else + '' + end + end + + def div_wrapper + template.content_tag(:div, :style => 'margin-left: 20%') do + file_field << existing_file_info.html_safe + end.html_safe + end + + def to_html + input_wrapping do + label_html << div_wrapper + end + end +end + diff --git a/lib/formtastic-better_file_inputs/image_field_input.rb b/lib/formtastic-better_file_inputs/image_field_input.rb new file mode 100644 index 0000000..58edc3c --- /dev/null +++ b/lib/formtastic-better_file_inputs/image_field_input.rb @@ -0,0 +1,6 @@ +class ImageFieldInput < FileFieldInput + def preview + template.image_tag(@url, :height => 50) + end +end + diff --git a/lib/formtastic-better_file_inputs/railtie.rb b/lib/formtastic-better_file_inputs/railtie.rb new file mode 100644 index 0000000..ee5eed7 --- /dev/null +++ b/lib/formtastic-better_file_inputs/railtie.rb @@ -0,0 +1,13 @@ +module Formtastic + module BetterFileInputs + class Railtie < ::Rails::Railtie + initializer 'formtastic-better_file_inputs.initialize' do + if defined?(Formtastic::Inputs) + require 'formtastic-better_file_inputs/file_field_input' + require 'formtastic-better_file_inputs/image_field_input' + end + end + end + end +end + diff --git a/lib/formtastic-better_file_inputs/version.rb b/lib/formtastic-better_file_inputs/version.rb new file mode 100644 index 0000000..c52af73 --- /dev/null +++ b/lib/formtastic-better_file_inputs/version.rb @@ -0,0 +1,5 @@ +module Formtastic + module BetterFileInputs + VERSION = "0.0.1" + end +end