refile_images 0.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4347bce5ee07c8c3ac96334bc52d218f0f771a36
4
+ data.tar.gz: d24c48ab40dcf0605aad97dced03b100c1c50a68
5
+ SHA512:
6
+ metadata.gz: e06d72c4931d24e49b310bbe39e3144ac4a1584924d5f5da7099edfd5dd8966731b0fea11194c5d72356b956acb76f9237510307ddbbcdb8236bce34a863a4d0
7
+ data.tar.gz: 25aae135a74e327f6e978602dbcea8e9a9bf86c298e34fc4c8a4d4c64415e80e50c2b88fe76b56d59138ece6d6a74c4f847606edd3be355fe3f4502b7b5a994a
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2016 Steven Barragán
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/Rakefile ADDED
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
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
+
8
+ require "rspec/core/rake_task"
9
+ require "rubocop/rake_task"
10
+
11
+ RSpec::Core::RakeTask.new(:spec)
12
+ RuboCop::RakeTask.new
13
+
14
+ task default: %i(spec rubocop)
15
+
16
+ require "rdoc/task"
17
+
18
+ RDoc::Task.new(:rdoc) do |rdoc|
19
+ rdoc.rdoc_dir = "rdoc"
20
+ rdoc.title = "RefileImages"
21
+ rdoc.options << "--line-numbers"
22
+ rdoc.rdoc_files.include("README.rdoc")
23
+ rdoc.rdoc_files.include("lib/**/*.rb")
24
+ end
25
+
26
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
27
+ load "rails/tasks/engine.rake"
28
+
29
+ load "rails/tasks/statistics.rake"
30
+
31
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+ module RefileImages
3
+ class ApplicationController < ActionController::Base
4
+ protect_from_forgery with: :exception
5
+ end
6
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+ module RefileImages
3
+ module ImageAttachment
4
+ include Refile::AttachmentHelper
5
+
6
+ def attachment_image_tag(record, name = :file, *options, attachment: :file)
7
+ if record.is_a?(Image)
8
+ super(record, name, *options)
9
+ else
10
+ super(record.send(name), attachment, *options)
11
+ end
12
+ end
13
+
14
+ module FormBuilder
15
+ def attachment_field(method, **options)
16
+ super(method, options.reverse_merge(direct: true))
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+ class Image < ActiveRecord::Base
3
+ belongs_to :imageable, polymorphic: true
4
+
5
+ attachment :file, type: :image
6
+
7
+ def url(size, *options)
8
+ imageable.get_url(self, size, *options)
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>RefileImages</title>
5
+ <%= stylesheet_link_tag "refile_images/application", media: "all" %>
6
+ <%= javascript_include_tag "refile_images/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+ RefileImages::Engine.routes.draw do
3
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+ class CreateRefileImagesImages < ActiveRecord::Migration
3
+ def change
4
+ create_table :images do |t|
5
+ t.string :file_id
6
+ t.string :file_filename
7
+ t.integer :file_size
8
+ t.string :file_content_type
9
+
10
+ t.references :imageable, polymorphic: true, index: true
11
+
12
+ t.timestamps null: false
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+ require "rails"
3
+ require "refile_images/imageable"
4
+
5
+ module RefileImages
6
+ class Engine < ::Rails::Engine
7
+ isolate_namespace RefileImages
8
+
9
+ initializer "refile_images.setup" do
10
+ ActiveSupport.on_load :active_record do
11
+ ActiveRecord::Base.send(:include, RefileImages::Imageable)
12
+ end
13
+
14
+ ActionView::Base.send(:include, RefileImages::ImageAttachment)
15
+ ActionView::Helpers::FormBuilder.send(:include, RefileImages::ImageAttachment::FormBuilder)
16
+ end
17
+
18
+ config.generators do |g|
19
+ g.test_framework :rspec
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,96 @@
1
+ # frozen_string_literal: true
2
+ require "active_support/concern"
3
+ require "refile"
4
+ require "refile/rails"
5
+ require "refile/attachment/active_record"
6
+
7
+ module RefileImages
8
+ module Imageable
9
+ extend ActiveSupport::Concern
10
+ extend Refile::Attachment
11
+
12
+ module ClassMethods
13
+ def image(name, attachment: :file, append: false, defaults: {})
14
+ attachments = attachment.to_s.pluralize.to_sym
15
+ plural = name.to_s.pluralize.to_sym
16
+ singular = name.to_s.singularize.to_sym
17
+
18
+ has_many plural, -> { where "#{attachment}_filename" => singular },
19
+ as: :imageable,
20
+ class_name: "Image",
21
+ dependent: :destroy,
22
+ inverse_of: :imageable
23
+
24
+ accepts_attachments_for plural, attachment: attachment, append: append
25
+
26
+ if singular == name
27
+ alias_method :"#{ name }_attachment_definition", :"#{ plural }_#{ attachments }_attachment_definition"
28
+ alias_method :"#{ name }_data", :"#{ plural }_#{ attachments }_data"
29
+
30
+ define_method name do
31
+ send(plural).last
32
+ end
33
+
34
+ define_method :"#{ name }=" do |file|
35
+ file = "[#{file}]" if file.is_a?(String) && !file.match(/^\[/)
36
+
37
+ send("#{plural}_#{attachments}=", [file])
38
+ end
39
+
40
+ define_method :"#{ name }_url" do |*args|
41
+ send(name).send("#{attachment}_url", *args)
42
+ end
43
+ end
44
+
45
+ image_options[name] = defaults
46
+
47
+ define_method :"#{ plural }_#{ attachments }=" do |files|
48
+ cache, files = files.partition { |file| file.is_a?(String) }
49
+
50
+ cache = Refile.parse_json(cache.first)
51
+
52
+ if not append and (files.present? or cache.present?)
53
+ send("#{plural}=", [])
54
+ end
55
+
56
+ if files.empty? and cache.present?
57
+ cache.select(&:present?).each do |file|
58
+ send(plural).build(
59
+ attachment => file.to_json,
60
+ "#{attachment}_filename" => name
61
+ )
62
+ end
63
+ else
64
+ files.select(&:present?).each do |file|
65
+ send(plural).build(
66
+ attachment => file,
67
+ "#{attachment}_filename" => name
68
+ )
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ alias images image
75
+
76
+ def image_options
77
+ @image_options ||= {}
78
+ end
79
+ end
80
+
81
+ def get_url(image, size, *options)
82
+ Refile.attachment_url(
83
+ image,
84
+ :file,
85
+ image_config(image.file_filename)[size],
86
+ *options
87
+ )
88
+ end
89
+
90
+ private
91
+
92
+ def image_config(name)
93
+ self.class.image_options[name.to_sym]
94
+ end
95
+ end
96
+ end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module RefileImages
3
+ VERSION = "0.0.1"
4
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ require "refile_images/engine"
3
+
4
+ module RefileImages
5
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: refile_images
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Steven Barragán
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-12-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: refile
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.6.2
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.6.2
41
+ - !ruby/object:Gem::Dependency
42
+ name: sqlite3
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: shoulda-matchers
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.1'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.1'
97
+ - !ruby/object:Gem::Dependency
98
+ name: webmock
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rubocop
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0.45'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0.45'
125
+ description: Description of RefileImages.
126
+ email:
127
+ - me@steven.mx
128
+ executables: []
129
+ extensions: []
130
+ extra_rdoc_files: []
131
+ files:
132
+ - MIT-LICENSE
133
+ - Rakefile
134
+ - app/assets/stylesheets/refile_images/application.css
135
+ - app/controllers/refile_images/application_controller.rb
136
+ - app/helpers/refile_images/image_attachment.rb
137
+ - app/models/image.rb
138
+ - app/views/layouts/refile_images/application.html.erb
139
+ - config/routes.rb
140
+ - db/migrate/20161107201317_create_refile_images_images.rb
141
+ - lib/refile_images.rb
142
+ - lib/refile_images/engine.rb
143
+ - lib/refile_images/imageable.rb
144
+ - lib/refile_images/version.rb
145
+ homepage: http://github.com/stacksocial/refile_images
146
+ licenses:
147
+ - MIT
148
+ metadata: {}
149
+ post_install_message:
150
+ rdoc_options: []
151
+ require_paths:
152
+ - lib
153
+ required_ruby_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ required_rubygems_version: !ruby/object:Gem::Requirement
159
+ requirements:
160
+ - - ">="
161
+ - !ruby/object:Gem::Version
162
+ version: '0'
163
+ requirements: []
164
+ rubyforge_project:
165
+ rubygems_version: 2.5.1
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Summary of RefileImages.
169
+ test_files: []