has_filepicker_image 0.1.1 → 0.1.2
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/README.md +9 -5
- data/has_filepicker_image.gemspec +1 -0
- data/lib/has_filepicker_image/base.rb +4 -68
- data/lib/has_filepicker_image/helpers/form_builder_helper.rb +15 -62
- data/lib/has_filepicker_image/url_builder.rb +74 -0
- data/lib/has_filepicker_image/version.rb +1 -1
- data/lib/has_filepicker_image/view.rb +85 -0
- data/lib/has_filepicker_image.rb +2 -0
- data/spec/model_spec.rb +3 -3
- metadata +6 -9
data/README.md
CHANGED
@@ -1,9 +1,13 @@
|
|
1
|
-
# HasFilepickerImage
|
1
|
+
# HasFilepickerImage
|
2
|
+
|
3
|
+
[](https://travis-ci.org/polmiro/has_filepicker_image)
|
4
|
+
[](https://codeclimate.com/github/polmiro/has_filepicker_image)
|
5
|
+
[](https://coveralls.io/r/polmiro/has_filepicker_image)
|
6
|
+
[](http://badge.fury.io/rb/has_filepicker_image)
|
2
7
|
|
3
8
|
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
|
6
|
-
It also adds form helpers to nicely display and reload your images in you forms
|
9
|
+
This gem helps you get running with [filepicker.io](http://filepicker.io) in Rails with your ActiveRecord models. You will be able to easily define sizes and styles of your images.
|
10
|
+
It also adds form helpers to seamelessly pick and preview the images in you forms.
|
7
11
|
|
8
12
|
## Installation
|
9
13
|
|
@@ -136,7 +140,7 @@ With Rails form builders
|
|
136
140
|
<%= form_for @user do |f| %>
|
137
141
|
<%= f.label :filepicker_url, "Upload Your Avatar:" %>
|
138
142
|
<%= f.filepicker_image_field :filepicker_url, :delete_button_html => 'Esborrar' %>
|
139
|
-
<%= f.
|
143
|
+
<%= f.filepicker_image_field :filepicker_url, 'doc', :delete_button_html => 'Esborrar' %>
|
140
144
|
<%= f.submit %>
|
141
145
|
<% end %>
|
142
146
|
```
|
@@ -12,6 +12,7 @@ Gem::Specification.new do |s|
|
|
12
12
|
s.homepage = ""
|
13
13
|
s.summary = "Easily add filepicker images to your models"
|
14
14
|
s.description = "Easily add filepicker images to your models"
|
15
|
+
s.license = 'MIT'
|
15
16
|
|
16
17
|
s.files = `git ls-files`.split("\n")
|
17
18
|
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
@@ -3,83 +3,19 @@ module HasFilepickerImage
|
|
3
3
|
extend ActiveSupport::Concern
|
4
4
|
|
5
5
|
module ClassMethods
|
6
|
-
|
7
6
|
def has_filepicker_image(name, options = {})
|
8
7
|
cattr_accessor :has_filepicker_image_styles
|
9
8
|
self.has_filepicker_image_styles ||= {}
|
10
9
|
self.has_filepicker_image_styles.merge!(name.to_sym => options[:styles])
|
11
10
|
|
12
11
|
define_method name do |*args|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
args: args
|
12
|
+
UrlBuilder.new(
|
13
|
+
:url => read_attribute("#{name}_url"),
|
14
|
+
:styles => self.class.has_filepicker_image_styles[name],
|
15
|
+
:args => args
|
18
16
|
).url
|
19
17
|
end
|
20
18
|
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
19
|
end
|
84
20
|
|
85
21
|
end
|
@@ -5,76 +5,29 @@ module HasFilepickerImage
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def filepicker_field(attribute_name, *args)
|
8
|
+
raise ArgumentError.new if args.size > 2
|
9
|
+
|
8
10
|
config = Rails.application.config.has_filepicker_image
|
9
11
|
config_name = nil
|
10
12
|
opts = {}
|
11
13
|
|
12
|
-
case
|
13
|
-
|
14
|
-
when 1
|
15
|
-
if args[0].is_a?(Hash)
|
14
|
+
case
|
15
|
+
when args.size == 1 && args[0].is_a?(Hash)
|
16
16
|
opts = args[0]
|
17
|
-
|
17
|
+
when args.size == 1 && !args[0].is_a?(Hash)
|
18
18
|
config_name = args[0]
|
19
|
-
|
20
|
-
|
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
|
19
|
+
when args.size == 2
|
20
|
+
config_name, opts = args
|
37
21
|
end
|
38
22
|
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
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
|
-
hidden_field = if Rails::VERSION::MAJOR >= 4
|
62
|
-
ActionView::Helpers::Tags::HiddenField.new(
|
63
|
-
@object_name,
|
64
|
-
attribute_name,
|
65
|
-
@template,
|
66
|
-
:object => object
|
67
|
-
).render
|
68
|
-
else
|
69
|
-
ActionView::Helpers::InstanceTag.new(
|
70
|
-
@object_name,
|
71
|
-
attribute_name,
|
72
|
-
@template,
|
73
|
-
object
|
74
|
-
).to_input_field_tag('hidden')
|
75
|
-
end
|
76
|
-
|
77
|
-
buttons + preview + hidden_field
|
23
|
+
View.new(
|
24
|
+
@object_name,
|
25
|
+
attribute_name,
|
26
|
+
@template,
|
27
|
+
config.get_config(config_name)
|
28
|
+
.deep_merge(opts)
|
29
|
+
.merge(:object => object)
|
30
|
+
).render
|
78
31
|
end
|
79
32
|
|
80
33
|
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module HasFilepickerImage
|
2
|
+
class UrlBuilder
|
3
|
+
def initialize(options)
|
4
|
+
@url = options[:url]
|
5
|
+
@options = ApiOptions.new(options[:styles], *options[:args])
|
6
|
+
@asset_host = Rails.application.config.has_filepicker_image.asset_host
|
7
|
+
end
|
8
|
+
|
9
|
+
def url
|
10
|
+
replace_asset_host(@url) + query_component if @url.present?
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def replace_asset_host(url)
|
16
|
+
if @asset_host
|
17
|
+
uri = URI(url)
|
18
|
+
uri.host = @asset_host
|
19
|
+
uri.to_s
|
20
|
+
else
|
21
|
+
url
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def query_component
|
26
|
+
component = @options.with_conversion_options? ? '/convert' : ''
|
27
|
+
component + '?' + @options.sort.map { |k,v| "#{k}=#{v}" }.join('&')
|
28
|
+
end
|
29
|
+
|
30
|
+
class ApiOptions < HashWithIndifferentAccess
|
31
|
+
CONVERSION_OPTIONS = [:w, :h, :fit]
|
32
|
+
RETRIEVAL_OPTIONS = [:dl, :cache]
|
33
|
+
DEFAULT_OPTIONS = {:cache => true, :dl => 'false', :fit => 'max'}
|
34
|
+
ALL_OPTIONS = RETRIEVAL_OPTIONS + CONVERSION_OPTIONS
|
35
|
+
|
36
|
+
def initialize(styles, hash_or_style = {}, hash = {})
|
37
|
+
@styles = styles
|
38
|
+
|
39
|
+
options = if hash_or_style.is_a?(Hash)
|
40
|
+
hash_or_style.assert_valid_keys(*ALL_OPTIONS)
|
41
|
+
hash_or_style
|
42
|
+
else
|
43
|
+
hash.assert_valid_keys(*ALL_OPTIONS)
|
44
|
+
style_api_options(hash_or_style).merge(hash)
|
45
|
+
end
|
46
|
+
|
47
|
+
merge! options
|
48
|
+
merge! defaults.except *options.keys.map(&:to_sym)
|
49
|
+
end
|
50
|
+
|
51
|
+
def with_conversion_options?
|
52
|
+
(keys.map(&:to_sym) & CONVERSION_OPTIONS).present?
|
53
|
+
end
|
54
|
+
|
55
|
+
private
|
56
|
+
|
57
|
+
def defaults
|
58
|
+
if with_conversion_options?
|
59
|
+
DEFAULT_OPTIONS
|
60
|
+
else
|
61
|
+
DEFAULT_OPTIONS.slice(*RETRIEVAL_OPTIONS)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def style_api_options(style)
|
66
|
+
{
|
67
|
+
:w => @styles[style][0],
|
68
|
+
:h => @styles[style][1]
|
69
|
+
}
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
module HasFilepickerImage
|
2
|
+
class View
|
3
|
+
def initialize(object_name, attribute_name, template, options = {})
|
4
|
+
@object_name = object_name
|
5
|
+
@attribute_name = attribute_name
|
6
|
+
@template = template
|
7
|
+
@options = options
|
8
|
+
end
|
9
|
+
|
10
|
+
def render
|
11
|
+
buttons + preview + hidden_field
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def buttons
|
17
|
+
@template.content_tag(
|
18
|
+
:div,
|
19
|
+
pick_button + remove_button,
|
20
|
+
:class => 'filepicker-button'
|
21
|
+
)
|
22
|
+
end
|
23
|
+
|
24
|
+
def preview
|
25
|
+
@template.content_tag(:div, :class => 'filepicker-image', :style => (value.present? ? '' : 'display:none;')) do
|
26
|
+
if value.present?
|
27
|
+
# Render preview + Delete link
|
28
|
+
thumb_url = value + "/convert?w=260&h=180"
|
29
|
+
thumb_alt = "#{@attribute_name} thumbnail"
|
30
|
+
@template.image_tag(thumb_url, alt: thumb_alt )
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def hidden_field
|
36
|
+
if Rails::VERSION::MAJOR >= 4
|
37
|
+
ActionView::Helpers::Tags::HiddenField.new(
|
38
|
+
@object_name,
|
39
|
+
@attribute_name,
|
40
|
+
@template,
|
41
|
+
:object => object
|
42
|
+
).render
|
43
|
+
else
|
44
|
+
ActionView::Helpers::InstanceTag.new(
|
45
|
+
@object_name,
|
46
|
+
@attribute_name,
|
47
|
+
@template,
|
48
|
+
object
|
49
|
+
).to_input_field_tag('hidden')
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def pick_button
|
54
|
+
@template.content_tag(:a,
|
55
|
+
@options[:pick_button_html],
|
56
|
+
html_options.merge(
|
57
|
+
:href => '#',
|
58
|
+
:style => value.present? ? 'display:none;' : '',
|
59
|
+
:'data-action' => 'pickImage'
|
60
|
+
)
|
61
|
+
)
|
62
|
+
end
|
63
|
+
|
64
|
+
def remove_button
|
65
|
+
@template.link_to(
|
66
|
+
@options[:delete_button_html],
|
67
|
+
'#',
|
68
|
+
:style => value.present? ? '' : 'display:none;',
|
69
|
+
:'data-action' => 'removeImage'
|
70
|
+
)
|
71
|
+
end
|
72
|
+
|
73
|
+
def value
|
74
|
+
object.send(@attribute_name)
|
75
|
+
end
|
76
|
+
|
77
|
+
def object
|
78
|
+
@options[:object]
|
79
|
+
end
|
80
|
+
|
81
|
+
def html_options
|
82
|
+
@options[:html_options] || {}
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
data/lib/has_filepicker_image.rb
CHANGED
@@ -4,6 +4,8 @@ require 'active_record'
|
|
4
4
|
module HasFilepickerImage
|
5
5
|
require "has_filepicker_image/version"
|
6
6
|
require "has_filepicker_image/configuration"
|
7
|
+
require "has_filepicker_image/url_builder"
|
8
|
+
require "has_filepicker_image/view"
|
7
9
|
require "has_filepicker_image/base"
|
8
10
|
require "has_filepicker_image/helpers/view_helper"
|
9
11
|
require "has_filepicker_image/helpers/form_builder_helper"
|
data/spec/model_spec.rb
CHANGED
@@ -34,19 +34,19 @@ describe TestModel do
|
|
34
34
|
|
35
35
|
context "when called with options" do
|
36
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&
|
37
|
+
model.image(w: 1, h: 2, dl: true).should == 'http://filepicker.io/image/convert?cache=true&dl=true&fit=max&h=2&w=1'
|
38
38
|
end
|
39
39
|
end
|
40
40
|
|
41
41
|
context "when called with a style" do
|
42
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&
|
43
|
+
model.image(:small).should == 'http://filepicker.io/image/convert?cache=true&dl=false&fit=max&h=100&w=50'
|
44
44
|
end
|
45
45
|
end
|
46
46
|
|
47
47
|
context "when called with a style and options" do
|
48
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&
|
49
|
+
model.image(:small, fit: 'crop', cache: 'false').should == 'http://filepicker.io/image/convert?cache=false&dl=false&fit=crop&h=100&w=50'
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_filepicker_image
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-11-
|
12
|
+
date: 2013-11-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -129,13 +129,16 @@ files:
|
|
129
129
|
- lib/has_filepicker_image/helpers/formtastic_form_input.rb
|
130
130
|
- lib/has_filepicker_image/helpers/simple_form_input.rb
|
131
131
|
- lib/has_filepicker_image/helpers/view_helper.rb
|
132
|
+
- lib/has_filepicker_image/url_builder.rb
|
132
133
|
- lib/has_filepicker_image/version.rb
|
134
|
+
- lib/has_filepicker_image/view.rb
|
133
135
|
- spec/form_builder_spec.rb
|
134
136
|
- spec/model_spec.rb
|
135
137
|
- spec/spec_helper.rb
|
136
138
|
- vendor/assets/javascripts/filepicker_async.js.erb
|
137
139
|
homepage: ''
|
138
|
-
licenses:
|
140
|
+
licenses:
|
141
|
+
- MIT
|
139
142
|
post_install_message:
|
140
143
|
rdoc_options: []
|
141
144
|
require_paths:
|
@@ -146,18 +149,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
146
149
|
- - ! '>='
|
147
150
|
- !ruby/object:Gem::Version
|
148
151
|
version: '0'
|
149
|
-
segments:
|
150
|
-
- 0
|
151
|
-
hash: 3317842162528816829
|
152
152
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
153
153
|
none: false
|
154
154
|
requirements:
|
155
155
|
- - ! '>='
|
156
156
|
- !ruby/object:Gem::Version
|
157
157
|
version: '0'
|
158
|
-
segments:
|
159
|
-
- 0
|
160
|
-
hash: 3317842162528816829
|
161
158
|
requirements: []
|
162
159
|
rubyforge_project:
|
163
160
|
rubygems_version: 1.8.24
|