has_filepicker_image 0.1.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.
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ nbproject/*
5
+ bin
6
+ .rspec
7
+ .rbenv-version
8
+ coverage/
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,124 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ has_filepicker_image (0.1.0)
5
+ rails (>= 3.2.0)
6
+ railties (>= 3.1.0, < 5.0)
7
+
8
+ GEM
9
+ remote: http://rubygems.org/
10
+ specs:
11
+ actionmailer (3.2.11)
12
+ actionpack (= 3.2.11)
13
+ mail (~> 2.4.4)
14
+ actionpack (3.2.11)
15
+ activemodel (= 3.2.11)
16
+ activesupport (= 3.2.11)
17
+ builder (~> 3.0.0)
18
+ erubis (~> 2.7.0)
19
+ journey (~> 1.0.4)
20
+ rack (~> 1.4.0)
21
+ rack-cache (~> 1.2)
22
+ rack-test (~> 0.6.1)
23
+ sprockets (~> 2.2.1)
24
+ activemodel (3.2.11)
25
+ activesupport (= 3.2.11)
26
+ builder (~> 3.0.0)
27
+ activerecord (3.2.11)
28
+ activemodel (= 3.2.11)
29
+ activesupport (= 3.2.11)
30
+ arel (~> 3.0.2)
31
+ tzinfo (~> 0.3.29)
32
+ activeresource (3.2.11)
33
+ activemodel (= 3.2.11)
34
+ activesupport (= 3.2.11)
35
+ activesupport (3.2.11)
36
+ i18n (~> 0.6)
37
+ multi_json (~> 1.0)
38
+ arel (3.0.2)
39
+ builder (3.0.4)
40
+ coveralls (0.7.0)
41
+ multi_json (~> 1.3)
42
+ rest-client
43
+ simplecov (>= 0.7)
44
+ term-ansicolor
45
+ thor
46
+ diff-lcs (1.1.3)
47
+ erubis (2.7.0)
48
+ hike (1.2.1)
49
+ i18n (0.6.1)
50
+ journey (1.0.4)
51
+ json (1.7.6)
52
+ mail (2.4.4)
53
+ i18n (>= 0.4.0)
54
+ mime-types (~> 1.16)
55
+ treetop (~> 1.4.8)
56
+ mime-types (1.25)
57
+ multi_json (1.5.0)
58
+ polyglot (0.3.3)
59
+ rack (1.4.4)
60
+ rack-cache (1.2)
61
+ rack (>= 0.4)
62
+ rack-ssl (1.3.3)
63
+ rack
64
+ rack-test (0.6.2)
65
+ rack (>= 1.0)
66
+ rails (3.2.11)
67
+ actionmailer (= 3.2.11)
68
+ actionpack (= 3.2.11)
69
+ activerecord (= 3.2.11)
70
+ activeresource (= 3.2.11)
71
+ activesupport (= 3.2.11)
72
+ bundler (~> 1.0)
73
+ railties (= 3.2.11)
74
+ railties (3.2.11)
75
+ actionpack (= 3.2.11)
76
+ activesupport (= 3.2.11)
77
+ rack-ssl (~> 1.3.2)
78
+ rake (>= 0.8.7)
79
+ rdoc (~> 3.4)
80
+ thor (>= 0.14.6, < 2.0)
81
+ rake (10.0.3)
82
+ rdoc (3.12.1)
83
+ json (~> 1.4)
84
+ rest-client (1.6.7)
85
+ mime-types (>= 1.16)
86
+ rspec-core (2.12.2)
87
+ rspec-expectations (2.12.1)
88
+ diff-lcs (~> 1.1.3)
89
+ rspec-mocks (2.12.2)
90
+ rspec-rails (2.12.2)
91
+ actionpack (>= 3.0)
92
+ activesupport (>= 3.0)
93
+ railties (>= 3.0)
94
+ rspec-core (~> 2.12.0)
95
+ rspec-expectations (~> 2.12.0)
96
+ rspec-mocks (~> 2.12.0)
97
+ simplecov (0.7.1)
98
+ multi_json (~> 1.0)
99
+ simplecov-html (~> 0.7.1)
100
+ simplecov-html (0.7.1)
101
+ sprockets (2.2.2)
102
+ hike (~> 1.2)
103
+ multi_json (~> 1.0)
104
+ rack (~> 1.0)
105
+ tilt (~> 1.1, != 1.3.0)
106
+ sqlite3 (1.3.7)
107
+ term-ansicolor (1.2.2)
108
+ tins (~> 0.8)
109
+ thor (0.17.0)
110
+ tilt (1.3.3)
111
+ tins (0.12.0)
112
+ treetop (1.4.15)
113
+ polyglot
114
+ polyglot (>= 0.3.1)
115
+ tzinfo (0.3.38)
116
+
117
+ PLATFORMS
118
+ ruby
119
+
120
+ DEPENDENCIES
121
+ coveralls
122
+ has_filepicker_image!
123
+ rspec-rails
124
+ sqlite3
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2013 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,160 @@
1
+ # HasFilepickerImage [![Build Status](https://travis-ci.org/polmiro/has_filepicker_image.png)](https://travis-ci.org/polmiro/has_filepicker_image) [![Code Climate](https://codeclimate.com/github/polmiro/has_filepicker_image.png)](https://codeclimate.com/github/polmiro/has_filepicker_image) [![Coverage Status](https://coveralls.io/repos/polmiro/has_filepicker_image/badge.png)](https://coveralls.io/r/polmiro/has_filepicker_image)
2
+
3
+ This gem is mainly an extension of [filepicker-rails](https://github.com/Filepicker/filepicker-rails). It integrates url methods into the models and the form builders.
4
+ This gem helps you get running with [filepicker.io](http://filepicker.io) in Rails with your ActiveRecord models.
5
+ It provides methods for your models to easily retrieve sizes and styles of your images.
6
+ It also adds form helpers to nicely display and reload your images in you forms
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'has_filepicker_image'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+
19
+ ```
20
+ $ bundle
21
+ ```
22
+
23
+ Or install it yourself as:
24
+
25
+ ```
26
+ $ gem install has_filepicker_image
27
+ ```
28
+
29
+
30
+ Add the filepicker.io async javascript library to your layout head:
31
+
32
+ ```ruby
33
+ <%= filepicker_async_js_include_tag %>
34
+ ```
35
+ or your javascript asset pipeline manifest:
36
+ ```
37
+ //= require 'filepicker_async'
38
+ ```
39
+
40
+
41
+
42
+ Set your API Key in config/application.rb:
43
+ ```ruby
44
+ config.has_filepicker_image.api_key = "Your filepicker.io API Key"
45
+ ```
46
+
47
+ Set your asset host (if you are using a CDN):
48
+ ```ruby
49
+ config.has_filepicker_image.asset_host = "disdojo11oijdf.cloudfront.net"
50
+ ```
51
+
52
+ Set your custom default options for the image urls (you can override or add your own options, view pickAndStore options in filepicker documentation):
53
+ ```ruby
54
+
55
+ # Defaults:
56
+ # {
57
+ # :delete_button_html => 'Remove',
58
+ # :pick_button_html => 'Pick',
59
+ # :html_options => {
60
+ # :'data-location' => 'S3',
61
+ # :'data-extensions' => '.png,.jpg,.jpeg',
62
+ # :'data-services' => 'COMPUTER',
63
+ # :'onchange' => "HasFilepickerImage.previewPickedFile(event);"
64
+ # }
65
+ # }
66
+
67
+
68
+ config.has_filepicker_image.defaults.merge!(
69
+ :delete_button_html => 'Delete',
70
+ :pick_button_html => 'Add',
71
+ )
72
+ ```
73
+
74
+ If you have multiple configurations that you want to use, you can add them like this:
75
+ ```ruby
76
+ config.has_filepicker_image.add_config(
77
+ 'doc',
78
+ {
79
+ :delete_button_html => 'Delete',
80
+ :pick_button_html => 'Select',
81
+ :html_options => {
82
+ :'data-location' => 'S3',
83
+ :'data-extensions' => '.pdf',
84
+ :'data-services' => 'COMPUTER',
85
+ }
86
+ }
87
+ )
88
+ ```
89
+
90
+ ## Usage
91
+ ### Migration
92
+
93
+ Add a column to the model's table of type :string:
94
+
95
+ ```ruby
96
+ class AddAvatarUrlToUser < ActiveRecord::Migration
97
+ def up
98
+ add_column :user, :avatar_url, :string
99
+ end
100
+
101
+ def down
102
+ remove_column :user, :avatar_url
103
+ end
104
+ end
105
+ ```
106
+
107
+
108
+ ### In your model
109
+
110
+ ```ruby
111
+ class User < ActiveRecord::Base
112
+ attr_accessible :avatar
113
+ has_filepicker_image :avatar, styles: { medium: [300,300], thumb: [100,100] }
114
+ end
115
+ ```
116
+
117
+ ### In your views
118
+
119
+ Call filepicker_url on your model passing any of the styles you defined. You can any filepicker.io options [the filepicker.io documentation](https://developers.filepicker.io/docs/web/#fpurl-images).
120
+
121
+ ```erb
122
+ <%= image_tag @user.avatar(:thumb), options %> ---> 'http://filepicker.io/images?w=60&w=40&cache=true&dl=false' having defined styles: { thumb: [60,40]}
123
+ <%= image_tag @user.avatar(:thumb, fit: 'crop'), options %> ---> 'http://filepicker.io/images?w=60&w=40&cache=true&fit=crop&dl=false'
124
+ <%= image_tag @user.avatar(w: 10, h: 20), options %> ---> 'http://filepicker.io/images?w=10&h=20&cache=true&dl=false'
125
+ <%= image_tag @user.avatar(cache: 'false', dl: 'true'), options %> ---> 'http://filepicker.io/images?cache=false&dl=true'
126
+ ```
127
+ _Note 1: This method accepts a style plus optional paramters for the url or simply optional parameters for the url._
128
+ _Note 2: Cache headers are enabled and download is disabled by default.
129
+
130
+ ### In your forms
131
+
132
+ You can overwrite defaults when using the helpers.
133
+
134
+ With Rails form builders
135
+ ```erb
136
+ <%= form_for @user do |f| %>
137
+ <%= f.label :filepicker_url, "Upload Your Avatar:" %>
138
+ <%= f.filepicker_image_field :filepicker_url, :delete_button_html => 'Esborrar' %>
139
+ <%= f.filepicker_field :filepicker_url, 'doc', :delete_button_html => 'Esborrar' %>
140
+ <%= f.submit %>
141
+ <% end %>
142
+ ```
143
+
144
+ With simple_form
145
+ ```erb
146
+ <%= simple_form_for @user do |f| %>
147
+ <%= f.input :filepicker_url, as: :filepicker_image %>
148
+ <%= f.submit %>
149
+ <% end %>
150
+ ```
151
+
152
+ With formtastic
153
+ ```erb
154
+ <%= simple_form_for @user do |f| %>
155
+ <%= f.input :filepicker_url, as: :formtastic_filepicker_image %>
156
+ <%= f.submit %>
157
+ <% end %>
158
+ ```
159
+
160
+
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+ begin
8
+ require 'rdoc/task'
9
+ rescue LoadError
10
+ require 'rdoc/rdoc'
11
+ require 'rake/rdoctask'
12
+ RDoc::Task = Rake::RDocTask
13
+ end
14
+
15
+ RDoc::Task.new(:rdoc) do |rdoc|
16
+ rdoc.rdoc_dir = 'rdoc'
17
+ rdoc.title = 'HasFilepickerImage'
18
+ rdoc.options << '--line-numbers'
19
+ rdoc.rdoc_files.include('README.rdoc')
20
+ rdoc.rdoc_files.include('lib/**/*.rb')
21
+ end
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ require 'rspec/core/rake_task'
26
+ task :default => :spec
27
+ RSpec::Core::RakeTask.new
28
+
@@ -0,0 +1,28 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ # Maintain your gem's version:
4
+ require "has_filepicker_image/version"
5
+
6
+ # Describe your gem and declare its dependencies:
7
+ Gem::Specification.new do |s|
8
+ s.name = "has_filepicker_image"
9
+ s.version = HasFilepickerImage::VERSION
10
+ s.authors = ["Pol"]
11
+ s.email = ["polmiro@gmail.com"]
12
+ s.homepage = ""
13
+ s.summary = "Easily add filepicker images to your models"
14
+ s.description = "Easily add filepicker images to your models"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_development_dependency "rspec-rails"
22
+ s.add_development_dependency "sqlite3"
23
+ s.add_development_dependency "coveralls"
24
+
25
+ s.add_dependency "railties", ">= 3.1.0", "< 5.0"
26
+ s.add_dependency "rails", '>=3.2.0'
27
+
28
+ end
@@ -0,0 +1,86 @@
1
+ module HasFilepickerImage
2
+ module Base
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+
7
+ def has_filepicker_image(name, options = {})
8
+ cattr_accessor :has_filepicker_image_styles
9
+ self.has_filepicker_image_styles ||= {}
10
+ self.has_filepicker_image_styles.merge!(name.to_sym => options[:styles])
11
+
12
+ define_method name do |*args|
13
+ HasFilepickerImageUrlService.new(
14
+ name: name,
15
+ url: read_attribute("#{name}_url"),
16
+ styles: self.class.has_filepicker_image_styles[name],
17
+ args: args
18
+ ).url
19
+ end
20
+ end
21
+
22
+ class HasFilepickerImageUrlService
23
+ def initialize(params)
24
+ @name = params[:name]
25
+ @base_url = params[:url]
26
+ @styles = params[:styles]
27
+ @retrieval_options, @conversion_options = parse_options(*params[:args])
28
+ end
29
+
30
+ def url
31
+ replace_asset_host(@base_url) + query_component if @base_url.present?
32
+ end
33
+
34
+ private
35
+
36
+ def replace_asset_host(url)
37
+ asset_host = Rails.application.config.has_filepicker_image.asset_host
38
+ if asset_host
39
+ uri = URI(url)
40
+ uri.host = asset_host
41
+ uri.to_s
42
+ else
43
+ url
44
+ end
45
+ end
46
+
47
+ def parse_options(*args)
48
+ retrieval_options = HashWithIndifferentAccess.new(:cache => true, :dl => 'false')
49
+ conversion_options = HashWithIndifferentAccess.new
50
+
51
+ if args.size > 2
52
+ raise 'Wrong number of arguments' if args.size > 2
53
+ elsif args.size > 0
54
+ hash = {}
55
+ arg = args[0]
56
+ if arg.is_a?(Hash)
57
+ hash.merge!(arg)
58
+ else
59
+ hash.merge!(args[1]) if args[1]
60
+ hash[:w] = @styles[arg][0]
61
+ hash[:h] = @styles[arg][1]
62
+ end
63
+ hash.assert_valid_keys(:w, :h, :fit, :dl, :cache)
64
+ retrieval_options.merge!(hash.slice(:dl, :cache))
65
+ conversion_options.merge!(hash.slice(:w, :h, :fit))
66
+ conversion_options[:fit] ||= 'max' if conversion_options.present?
67
+ end
68
+
69
+ [retrieval_options, conversion_options]
70
+ end
71
+
72
+ def query_component
73
+ component = @conversion_options.present? ? '/convert' : ''
74
+ component + '?' + all_options.map { |k,v| "#{k}=#{v}" }.join('&')
75
+ end
76
+
77
+ def all_options
78
+ @retrieval_options.merge(@conversion_options)
79
+ end
80
+
81
+ end
82
+
83
+ end
84
+
85
+ end
86
+ end
@@ -0,0 +1,52 @@
1
+ module HasFilepickerImage
2
+ class Configuration
3
+ attr_writer :api_key
4
+ attr_accessor :asset_host
5
+
6
+ def initialize(*args)
7
+ @conf = HashWithIndifferentAccess.new
8
+
9
+ @conf[:default] = {
10
+ :delete_button_html => 'Remove',
11
+ :pick_button_html => 'Pick',
12
+ :html_options => {
13
+ :'data-location' => 'S3',
14
+ :'data-extensions' => '.png,.jpg,.jpeg',
15
+ :'data-services' => 'COMPUTER',
16
+ :'onchange' => "HasFilepickerImage.previewPickedFile(event);"
17
+ }
18
+ }
19
+
20
+ if ::Rails.env.development? || ::Rails.env.test?
21
+ @conf[:default][:html_options][:'data-debug'] = true
22
+ end
23
+ end
24
+
25
+ def api_key
26
+ @api_key or raise "Set config.has_filepicker_image.api_key"
27
+ end
28
+
29
+ def defaults
30
+ @conf[:default]
31
+ end
32
+
33
+ def defaults=(opts)
34
+ @conf[:default] = opts
35
+ end
36
+
37
+ def add_config(name, value)
38
+ @conf[name] = value
39
+ end
40
+
41
+ def get_config(name = nil)
42
+ if name
43
+ unless @conf.has_key?(name)
44
+ raise 'HasFilepickerImage configuration does not exist'
45
+ end
46
+ @conf[name]
47
+ else
48
+ defaults
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,19 @@
1
+ module HasFilepickerImage
2
+ class Engine < ::Rails::Engine
3
+
4
+ config.has_filepicker_image = HasFilepickerImage::Configuration.new
5
+
6
+ ActiveSupport.on_load(:action_view) do
7
+ class ActionView::Base
8
+ include HasFilepickerImage::ViewHelper
9
+ end
10
+
11
+ class ActionView::Helpers::FormBuilder
12
+ include HasFilepickerImage::FormBuilderHelper
13
+ end
14
+ end
15
+
16
+ ActiveRecord::Base.send(:include, HasFilepickerImage::Base)
17
+
18
+ end
19
+ end
@@ -0,0 +1,69 @@
1
+ module HasFilepickerImage
2
+ module FormBuilderHelper
3
+ def filepicker_image_field(attribute_name, *args)
4
+ filepicker_field(attribute_name, *args)
5
+ end
6
+
7
+ def filepicker_field(attribute_name, *args)
8
+ config = Rails.application.config.has_filepicker_image
9
+ config_name = nil
10
+ opts = {}
11
+
12
+ case args.size
13
+ when 0
14
+ when 1
15
+ if args[0].is_a?(Hash)
16
+ opts = args[0]
17
+ else
18
+ config_name = args[0]
19
+ end
20
+ when 2
21
+ config_name, opts = args
22
+ else
23
+ raise ArgumentError.new('Wrong number of arguments for filepicker_field')
24
+ end
25
+
26
+ value = object.send(attribute_name)
27
+ options = config.get_config(config_name).deep_merge(opts)
28
+ html_options = options[:html_options] || {}
29
+
30
+ preview = @template.content_tag(:div, :class => 'filepicker-image', :style => (value.present? ? '' : 'display:none;')) do
31
+ if value.present?
32
+ # Render preview + Delete link
33
+ thumb_url = value + "/convert?w=260&h=180"
34
+ thumb_alt = "#{attribute_name} thumbnail"
35
+ @template.image_tag(thumb_url, alt: thumb_alt )
36
+ end
37
+ end
38
+
39
+ pick_button = @template.content_tag(:a,
40
+ options[:pick_button_html],
41
+ html_options.merge(
42
+ :href => '#',
43
+ :style => value.present? ? 'display:none;' : '',
44
+ :'data-action' => 'pickImage'
45
+ )
46
+ )
47
+
48
+ remove_button = @template.link_to(
49
+ options[:delete_button_html],
50
+ '#',
51
+ :style => value.present? ? '' : 'display:none;',
52
+ :'data-action' => 'removeImage'
53
+ )
54
+
55
+ buttons = @template.content_tag(
56
+ :div,
57
+ pick_button + remove_button,
58
+ :class => 'filepicker-button'
59
+ )
60
+
61
+ buttons + preview + ActionView::Helpers::InstanceTag.new(
62
+ @object_name,
63
+ attribute_name,
64
+ @template,
65
+ object
66
+ ).to_input_field_tag('hidden')
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,10 @@
1
+ class FormtasticFilepickerImageInput
2
+ include Formtastic::Inputs::Base
3
+
4
+ def to_html
5
+ input_wrapping do
6
+ label_html <<
7
+ builder.filepicker_image_field(method, input_html_options)
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,10 @@
1
+ module SimpleForm
2
+ module Inputs
3
+ class FilepickerImageInput < Base
4
+
5
+ def input
6
+ @builder.filepicker_image_field(attribute_name, input_html_options)
7
+ end
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,7 @@
1
+ module HasFilepickerImage
2
+ module ViewHelper
3
+ def filepicker_async_js_include_tag
4
+ javascript_include_tag 'filepicker_async'
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ module HasFilepickerImage
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,13 @@
1
+ require 'rails'
2
+ require 'active_record'
3
+
4
+ module HasFilepickerImage
5
+ require "has_filepicker_image/version"
6
+ require "has_filepicker_image/configuration"
7
+ require "has_filepicker_image/base"
8
+ require "has_filepicker_image/helpers/view_helper"
9
+ require "has_filepicker_image/helpers/form_builder_helper"
10
+ require 'has_filepicker_image/helpers/simple_form_input' if defined? SimpleForm
11
+ require 'has_filepicker_image/helpers/formtastic_form_input' if defined? Formtastic
12
+ require "has_filepicker_image/engine"
13
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe HasFilepickerImage::FormBuilderHelper do
4
+ class FormBuilder < ActionView::Helpers::FormBuilder
5
+ include HasFilepickerImage::FormBuilderHelper
6
+ end
7
+
8
+ describe "#filepicker_image_field" do
9
+ before do
10
+ @configuration = HasFilepickerImage::Configuration.new
11
+ @configuration.defaults = {
12
+ :pick_button_html => 'Pick',
13
+ :delete_button_html => 'Remove'
14
+ }
15
+ allow_message_expectations_on_nil
16
+ ::Rails.application.stub_chain(:config, :has_filepicker_image).and_return(@configuration)
17
+ end
18
+
19
+ let(:model) { TestModel.new }
20
+ let(:builder) { FormBuilder.new(:test_model, model, ActionView::Base.new, {}, proc {}) }
21
+
22
+ it "should add a filepicker_image_field method" do
23
+ builder.should respond_to(:filepicker_image_field)
24
+ end
25
+
26
+ it "returns the filepicker input field" do
27
+ builder.filepicker_image_field(:image_url).should ==
28
+ "<div class=\"filepicker-button\">" +
29
+ "<a data-action=\"pickImage\" href=\"#\" style=\"\">Pick</a>" +
30
+ "<a href=\"#\" data-action=\"removeImage\" style=\"display:none;\">Remove</a>" +
31
+ "</div>" +
32
+ "<div class=\"filepicker-image\" style=\"display:none;\"></div>" +
33
+ "<input id=\"test_model_image_url\" name=\"test_model[image_url]\" type=\"hidden\" />"
34
+ end
35
+
36
+ it "returns the filepicker input with preview" do
37
+ model.image_url = 'http://filepicker.io/images/1'
38
+ builder.filepicker_image_field(:image_url).should ==
39
+ "<div class=\"filepicker-button\">" +
40
+ "<a data-action=\"pickImage\" href=\"#\" style=\"display:none;\">Pick</a>" +
41
+ "<a href=\"#\" data-action=\"removeImage\" style=\"\">Remove</a>" +
42
+ "</div>" +
43
+ "<div class=\"filepicker-image\" style=\"\"><img alt=\"image_url thumbnail\" src=\"http://filepicker.io/images/1/convert?w=260&amp;h=180\" /></div>" +
44
+ "<input id=\"test_model_image_url\" name=\"test_model[image_url]\" type=\"hidden\" value=\"http://filepicker.io/images/1\" />"
45
+ end
46
+
47
+ it "returns the filepicker input with defaults options" do
48
+ @configuration.defaults = @configuration.defaults.deep_merge(
49
+ :html_options => { :'data-fp-debug' => true }
50
+ )
51
+
52
+ builder.filepicker_image_field(:image_url).should ==
53
+ "<div class=\"filepicker-button\">" +
54
+ "<a data-action=\"pickImage\" data-fp-debug=\"true\" href=\"#\" style=\"\">Pick</a>" +
55
+ "<a href=\"#\" data-action=\"removeImage\" style=\"display:none;\">Remove</a>" +
56
+ "</div>" +
57
+ "<div class=\"filepicker-image\" style=\"display:none;\"></div>" +
58
+ "<input id=\"test_model_image_url\" name=\"test_model[image_url]\" type=\"hidden\" />"
59
+ end
60
+
61
+ it "returns the filepicker input with custom options" do
62
+ @configuration.defaults = {
63
+ :pick_button_html => 'Add Photo',
64
+ :delete_button_html => 'Remove Photo'
65
+ }
66
+
67
+ builder.filepicker_image_field(:image_url).should ==
68
+ "<div class=\"filepicker-button\">" +
69
+ "<a data-action=\"pickImage\" href=\"#\" style=\"\">Add Photo</a>" +
70
+ "<a href=\"#\" data-action=\"removeImage\" style=\"display:none;\">Remove Photo</a>" +
71
+ "</div>" +
72
+ "<div class=\"filepicker-image\" style=\"display:none;\"></div>" +
73
+ "<input id=\"test_model_image_url\" name=\"test_model[image_url]\" type=\"hidden\" />"
74
+ end
75
+
76
+
77
+ it "returns input with a different config" do
78
+ @configuration.add_config(
79
+ :picture_config,
80
+ :pick_button_html => 'Add Picture',
81
+ :delete_button_html => 'Remove Picture'
82
+ )
83
+
84
+ builder.filepicker_image_field(:image_url, :picture_config).should ==
85
+ "<div class=\"filepicker-button\">" +
86
+ "<a data-action=\"pickImage\" href=\"#\" style=\"\">Add Picture</a>" +
87
+ "<a href=\"#\" data-action=\"removeImage\" style=\"display:none;\">Remove Picture</a>" +
88
+ "</div>" +
89
+ "<div class=\"filepicker-image\" style=\"display:none;\"></div>" +
90
+ "<input id=\"test_model_image_url\" name=\"test_model[image_url]\" type=\"hidden\" />"
91
+ end
92
+
93
+ end
94
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe TestModel do
4
+
5
+ before(:each) do
6
+ clean_database
7
+ allow_message_expectations_on_nil
8
+ ::Rails.application.stub_chain(:config, :has_filepicker_image, :asset_host).and_return(nil)
9
+ end
10
+
11
+ let(:model) { TestModel.new(image_url: 'http://filepicker.io/image') }
12
+
13
+ describe "#image" do
14
+ context "when no url" do
15
+ it "returns nil" do
16
+ model.image_url = nil
17
+ model.image.should be_nil
18
+ end
19
+ end
20
+
21
+ context "when CDN configured" do
22
+ it "returns the url replacing the host for the CDN" do
23
+ cdn = 'cdn.net'
24
+ ::Rails.application.stub_chain(:config, :has_filepicker_image, :asset_host).and_return(cdn)
25
+ model.image.should == 'http://cdn.net/image?cache=true&dl=false'
26
+ end
27
+ end
28
+
29
+ context "when called without arguments" do
30
+ it "returns the original url with cache=true and dl=false by default" do
31
+ model.image.should == 'http://filepicker.io/image?cache=true&dl=false'
32
+ end
33
+ end
34
+
35
+ context "when called with options" do
36
+ it "returns the url with the options" do
37
+ model.image(w: 1, h: 2, dl: true).should == 'http://filepicker.io/image/convert?cache=true&dl=true&w=1&h=2&fit=max'
38
+ end
39
+ end
40
+
41
+ context "when called with a style" do
42
+ it "returns the url with the dimensions of the style and fit max by default" do
43
+ model.image(:small).should == 'http://filepicker.io/image/convert?cache=true&dl=false&w=50&h=100&fit=max'
44
+ end
45
+ end
46
+
47
+ context "when called with a style and options" do
48
+ it "returns the url with the dimensions of the style and the provided options" do
49
+ model.image(:small, fit: 'crop', cache: 'false').should == 'http://filepicker.io/image/convert?cache=false&dl=false&w=50&h=100&fit=crop'
50
+ end
51
+ end
52
+ end
53
+
54
+ end
@@ -0,0 +1,31 @@
1
+ require 'coveralls'
2
+ Coveralls.wear!
3
+
4
+ $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
5
+
6
+ require 'rails/all'
7
+ require 'rspec/rails'
8
+ require 'has_filepicker_image'
9
+
10
+
11
+ ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
12
+
13
+ ActiveRecord::Schema.define(version: 1) do
14
+ create_table :test_models do |t|
15
+ t.string :image_url
16
+ end
17
+ end
18
+
19
+ class TestModel < ActiveRecord::Base
20
+ has_filepicker_image :image, styles: {
21
+ small: [50,100],
22
+ normal: [200,400]
23
+ }
24
+ end
25
+
26
+ def clean_database
27
+ models = [TestModel]
28
+ models.each do |model|
29
+ ActiveRecord::Base.connection.execute "DELETE FROM #{model.table_name}"
30
+ end
31
+ end
@@ -0,0 +1,62 @@
1
+ (function(a){if(window.filepicker){return}var b=a.createElement("script");b.type="text/javascript";b.async=!0;b.src=("https:"===a.location.protocol?"https:":"http:")+"//api.filepicker.io/v1/filepicker.js";var c=a.getElementsByTagName("script")[0];c.parentNode.insertBefore(b,c);var d={};d._queue=[];var e="pick,pickMultiple,pickAndStore,read,write,writeUrl,export,convert,store,storeUrl,remove,stat,setKey,constructWidget,makeDropPane".split(",");var f=function(a,b){return function(){b.push([a,arguments])}};for(var g=0;g<e.length;g++){d[e[g]]=f(e[g],d._queue)}window.filepicker=d})(document);
2
+
3
+ filepicker.setKey('<%= ::Rails.application.config.has_filepicker_image.api_key %>');
4
+
5
+ filepicker.previewPickedImage = function($container, url) {
6
+ var $imageContainer = $container.find('.filepicker-image');
7
+ var img = $("<img src='" + url + '/convert?w=50&h=50' + '\'/>');
8
+ $imageContainer.html(img);
9
+ return true;
10
+ };
11
+
12
+ filepicker.removeImage = function($button) {
13
+ var $container = $button.parent().parent();
14
+ $container.find('input[type=hidden]').val('');
15
+ $container.find('.filepicker-image').html('').hide();
16
+ $container.find('[data-action=removeImage]').hide();
17
+ $container.find('[data-action=pickImage]').show();
18
+ };
19
+
20
+ filepicker.pickImage = function($button) {
21
+ var data = $button.data();
22
+ var $container = $button.parent().parent();
23
+ filepicker.pickAndStore(
24
+ data,
25
+ data,
26
+ function(fpfiles) {
27
+ var url = fpfiles[0].url;
28
+ $container.find('input[type=hidden]').val(url);
29
+ filepicker.previewPickedImage($container, url);
30
+ $container.find('[data-action=removeImage]').show();
31
+ $container.find('.filepicker-image').show();
32
+ $button.hide();
33
+ },
34
+ function(fpfiles) {
35
+ alert('Error uploading file');
36
+ }
37
+ );
38
+ };
39
+
40
+ $(document).on('click', '.has_many.attachments .button', function(event) {
41
+ $input_element = $(this).closest('.has_many.attachments').find("[type='filepicker']:last");
42
+ filepicker.pickImange( $element );
43
+ });
44
+
45
+ $(document).on('nested:fieldAdded', function(event) {
46
+ var field = event.field;
47
+ var $element = field.find("[data-action=pickImage]");
48
+ if ($element.size() > 0)
49
+ filepicker.pickImage( $element );
50
+ });
51
+
52
+ $(document).on('click', 'a[data-action=pickImage]', function(event) {
53
+ event.preventDefault();
54
+ filepicker.pickImage( $(this) );
55
+ });
56
+
57
+ $(document).on('click', 'a[data-action=removeImage]', function(event) {
58
+ event.preventDefault();
59
+
60
+ filepicker.removeImage( $(this) )
61
+ });
62
+
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_filepicker_image
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Pol
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-11-04 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rspec-rails
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sqlite3
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: coveralls
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: railties
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: 3.1.0
70
+ - - <
71
+ - !ruby/object:Gem::Version
72
+ version: '5.0'
73
+ type: :runtime
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ none: false
77
+ requirements:
78
+ - - ! '>='
79
+ - !ruby/object:Gem::Version
80
+ version: 3.1.0
81
+ - - <
82
+ - !ruby/object:Gem::Version
83
+ version: '5.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rails
86
+ requirement: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: 3.2.0
92
+ type: :runtime
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: 3.2.0
100
+ description: Easily add filepicker images to your models
101
+ email:
102
+ - polmiro@gmail.com
103
+ executables: []
104
+ extensions: []
105
+ extra_rdoc_files: []
106
+ files:
107
+ - .gitignore
108
+ - .travis.yml
109
+ - Gemfile
110
+ - Gemfile.lock
111
+ - MIT-LICENSE
112
+ - README.md
113
+ - Rakefile
114
+ - has_filepicker_image.gemspec
115
+ - lib/has_filepicker_image.rb
116
+ - lib/has_filepicker_image/base.rb
117
+ - lib/has_filepicker_image/configuration.rb
118
+ - lib/has_filepicker_image/engine.rb
119
+ - lib/has_filepicker_image/helpers/form_builder_helper.rb
120
+ - lib/has_filepicker_image/helpers/formtastic_form_input.rb
121
+ - lib/has_filepicker_image/helpers/simple_form_input.rb
122
+ - lib/has_filepicker_image/helpers/view_helper.rb
123
+ - lib/has_filepicker_image/version.rb
124
+ - spec/form_builder_spec.rb
125
+ - spec/model_spec.rb
126
+ - spec/spec_helper.rb
127
+ - vendor/assets/javascripts/filepicker_async.js.erb
128
+ homepage: ''
129
+ licenses: []
130
+ post_install_message:
131
+ rdoc_options: []
132
+ require_paths:
133
+ - lib
134
+ required_ruby_version: !ruby/object:Gem::Requirement
135
+ none: false
136
+ requirements:
137
+ - - ! '>='
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ required_rubygems_version: !ruby/object:Gem::Requirement
141
+ none: false
142
+ requirements:
143
+ - - ! '>='
144
+ - !ruby/object:Gem::Version
145
+ version: '0'
146
+ requirements: []
147
+ rubyforge_project:
148
+ rubygems_version: 1.8.24
149
+ signing_key:
150
+ specification_version: 3
151
+ summary: Easily add filepicker images to your models
152
+ test_files:
153
+ - spec/form_builder_spec.rb
154
+ - spec/model_spec.rb
155
+ - spec/spec_helper.rb