{"action":"created","comment":{"url":"https://api.github.com/repos/thoughtbot/paperclip/pulls/comments/31386584","id":31386584,"diff_hunk":"@@ -31,9 +31,112 @@\n     file.close\n   end\n \n+  context \"when generators and procesors are provided\" do\n+    class TestProcessor < Paperclip::Processor\n+      def make\n+        original_content = file.tap(&:rewind).read\n+        file.rewind\n+        result = Tempfile.new(\"paperclip-test\")\n+        result.print \"#{original_content}#{options[:suffix]}\"\n+        result.tap(&:rewind)\n+      end\n+    end\n+\n+    class TestFullGenerator < Paperclip::Processor\n+      def make\n+        results = {}\n+        original_content = file.tap(&:rewind).read\n+        file.rewind\n+        options.each do |name, style_options|\n+          result = Tempfile.new(\"paperclip-test\")\n+          result.print \"#{original_content}#{style_options[:suffix].upcase}\"\n+          results[name] = result.tap(&:rewind)\n+        end\n+        results\n+      end\n+    end\n+\n+    class TestPartGenerator < Paperclip::Processor\n+      def make\n+        original_content = file.tap(&:rewind).read\n+        file.rewind\n+        name, style_options = *options.first\n+        result = Tempfile.new(\"paperclip-test\")\n+        result.print \"#{original_content}#{style_options[:suffix].upcase}\"\n+        { name => result.tap(&:rewind) }\n+      end\n+    end\n+\n+    before do\n+      Paperclip.register_processor :test_full_generator, TestFullGenerator\n+      Paperclip.register_processor :test_part_generator, TestPartGenerator\n+      Paperclip.register_processor :test_processor, TestProcessor\n+    end\n+\n+    after { Paperclip.clear_processors! }\n+\n+    it \"runs the processors and generator at the right times\" do\n+      rebuild_model(\n+        styles: { original: { suffix: \"o\" }, a: { suffix: \"a\" }, b: { suffix: \"b\" } },\n+        generator: :test_full_generator,\n+        processors: [:test_processor],\n+      )\n+      tempfile = Tempfile.open('paperclip-test') do |file|\n+        file.print '.'\n+        file\n+      end\n+      open(tempfile.path) do |file|\n+        dummy = Dummy.new\n+        dummy.avatar = file\n+        dummy.save\n+        assert_equal '.oAa', File.read(dummy.avatar.path(:a))\n+        assert_equal '.oBb', File.read(dummy.avatar.path(:b))\n+        assert_equal '.o', File.read(dummy.avatar.path(:original))\n+      end\n+    end\n+\n+    it \"uses the original for styles not generated by the generator\" do\n+      rebuild_model(\n+        styles: { original: { suffix: \"o\" }, a: { suffix: \"a\" }, b: { suffix: \"b\" } },\n+        generator: :test_part_generator,\n+        processors: [:test_processor],\n+      )\n+      tempfile = Tempfile.open('paperclip-test') do |file|\n+        file.print '.'","path":"spec/paperclip/attachment_spec.rb","position":86,"original_position":86,"commit_id":"75926edf20ddadd82a4221ff366f432d3dc07d3a","original_commit_id":"75926edf20ddadd82a4221ff366f432d3dc07d3a","user":{"login":"houndci","id":6697940,"avatar_url":"https://avatars.githubusercontent.com/u/6697940?v=3","gravatar_id":"","url":"https://api.github.com/users/houndci","html_url":"https://github.com/houndci","followers_url":"https://api.github.com/users/houndci/followers","following_url":"https://api.github.com/users/houndci/following{/other_user}","gists_url":"https://api.github.com/users/houndci/gists{/gist_id}","starred_url":"https://api.github.com/users/houndci/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/houndci/subscriptions","organizations_url":"https://api.github.com/users/houndci/orgs","repos_url":"https://api.github.com/users/houndci/repos","events_url":"https://api.github.com/users/houndci/events{/privacy}","received_events_url":"https://api.github.com/users/houndci/received_events","type":"User","site_admin":false},"body":"Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.","created_at":"2015-05-31T03:39:34Z","updated_at":"2015-05-31T03:39:34Z","html_url":"https://github.com/thoughtbot/paperclip/pull/1360#discussion_r31386584","pull_request_url":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360","_links":{"self":{"href":"https://api.github.com/repos/thoughtbot/paperclip/pulls/comments/31386584"},"html":{"href":"https://github.com/thoughtbot/paperclip/pull/1360#discussion_r31386584"},"pull_request":{"href":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360"}}},"pull_request":{"url":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360","id":9216651,"html_url":"https://github.com/thoughtbot/paperclip/pull/1360","diff_url":"https://github.com/thoughtbot/paperclip/pull/1360.diff","patch_url":"https://github.com/thoughtbot/paperclip/pull/1360.patch","issue_url":"https://api.github.com/repos/thoughtbot/paperclip/issues/1360","number":1360,"state":"open","locked":false,"title":"Support generating multiple styles at once","user":{"login":"oggy","id":11112,"avatar_url":"https://avatars.githubusercontent.com/u/11112?v=3","gravatar_id":"","url":"https://api.github.com/users/oggy","html_url":"https://github.com/oggy","followers_url":"https://api.github.com/users/oggy/followers","following_url":"https://api.github.com/users/oggy/following{/other_user}","gists_url":"https://api.github.com/users/oggy/gists{/gist_id}","starred_url":"https://api.github.com/users/oggy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/oggy/subscriptions","organizations_url":"https://api.github.com/users/oggy/orgs","repos_url":"https://api.github.com/users/oggy/repos","events_url":"https://api.github.com/users/oggy/events{/privacy}","received_events_url":"https://api.github.com/users/oggy/received_events","type":"User","site_admin":false},"body":"This patch adds an optional \"generation\" step that occurs *after* processing the original file, *before* the result is handed to the processor pipeline for non-original styles. In picture form:\r\n\r\nCurrent paperclip:\r\n\r\n```\r\n                                    +------> style1 --> [style1 file]\r\n                                   /       processors\r\n                                  /     \r\n  [original] -> original --------+---------> style2 --> [style2 file]\r\n               processors         \\        processors\r\n                                   \\\r\n                                    +------> style3 --> [style3 file]\r\n                                           processors\r\n```\r\n\r\nWith this patch:\r\n\r\n```\r\n                                    +------> style1 --> [style1 file]\r\n                                   /       processors\r\n                                  /     \r\n  [original] -> original -> generator -----> style2 --> [style2 file]\r\n               processors         \\        processors\r\n                                   \\\r\n                                    +------> style3 --> [style3 file]\r\n                                           processors\r\n```\r\n\r\nUnlike the processors -- which each take 1 input file and produce 1 output file -- a generator takes 1 input file and produces up to N output files (N = number of styles). If any styles are missing from the output of the generator, the processed original file is simply passed on to the ungenerated style's processors (as if the generator was the identity transform for these styles).\r\n\r\nIn code, a generator is simply a processor which is specified via a :generator option to the attachment. The generator's `options` will be a hash of style name to style options, and `make` should return a hash of style name to result file.\r\n\r\n## Motivation\r\n\r\nImageMagick has the capability of generating multiple output files, and doing so can be much more efficient than doing them one at a time. The reasons should be pretty intuitive:\r\n\r\n* The OS spawns 1 process, not N.\r\n* The input file only has to be read from disk once, not N times.\r\n* Initial processing steps common to multiple styles can be performed once, not N times.\r\n\r\n## Benchmarks\r\n\r\n    $ ./bench.rb \r\n    Rehearsal --------------------------------------------\r\n    FastUser   0.070000   0.050000   1.050000 (  1.081597)\r\n    SlowUser   0.050000   0.040000   1.400000 (  1.428589)\r\n    ----------------------------------- total: 2.450000sec\r\n     \r\n                   user     system      total        real\r\n    FastUser   0.050000   0.050000   1.030000 (  1.085033)\r\n    SlowUser   0.040000   0.050000   1.420000 (  1.451335)\r\n\r\n    $ cat bench.rb \r\n    #!/usr/bin/env ruby\r\n     \r\n    require 'benchmark'\r\n    require_relative './config/environment'\r\n     \r\n    Benchmark.bmbm do |x|\r\n      x.report('FastUser') { open('input.jpg') { |f| FastUser.create(photo: f) } }\r\n      x.report('SlowUser') { open('input.jpg') { |f| SlowUser.create(photo: f) } }\r\n    end\r\n\r\n    $ cat app/models/fast_user.rb \r\n    class FastUser < ActiveRecord::Base\r\n      set_table_name 'users'\r\n      attr_accessible :photo\r\n      has_attached_file :photo, styles: {a: '100x100', b: '200x200', c: '400x400'}, generator: :photo_generator\r\n    end\r\n\r\n    $ cat app/models/slow_user.rb \r\n    class SlowUser < ActiveRecord::Base\r\n      set_table_name 'users'\r\n      attr_accessible :photo\r\n      has_attached_file :photo,\r\n        styles: {a: '100x100', b: '200x200', c: '400x400'},\r\n        convert_options: {all: \"-strip -quality 80\"}\r\n    end\r\n\r\n    $ cat lib/paperclip_processors/photo_generator.rb\r\n    require 'shellwords'\r\n\r\n    module Paperclip\r\n      class PhotoGenerator < Processor\r\n        def make\r\n          command = <<-EOS\r\n            convert #{file.path.shellescape}[0] -auto-orient -strip -quality 80\r\n            ( +clone -resize '100x100^' -write #{results[:a].path.shellescape} +delete )\r\n            ( +clone -resize '200x200^' -write #{results[:b].path.shellescape} +delete )\r\n                     -resize '300x300^'        #{results[:c].path.shellescape}\r\n          EOS\r\n          Paperclip.run command.shellsplit.shelljoin\r\n          results\r\n        end\r\n     \r\n        def results\r\n          @results ||= options.inject({}) do |results, (name, style)|\r\n            results[name] = Tempfile.new(['paperclip', '.jpg']).tap(&:binmode)\r\n            results\r\n          end\r\n        end\r\n      end\r\n    end\r\n\r\nSo this chops ~25% off the processing time. The input file used here was a 1.3MB, 2048x1536 JPG. Savings would increase with larger input files, when generating more styles, or with pipelines with more common leading steps.\r\n","created_at":"2013-10-18T17:43:24Z","updated_at":"2015-05-31T03:39:34Z","closed_at":null,"merged_at":null,"merge_commit_sha":"d1eb0ddf3bb078b40042bfb085c229116a7f77e9","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360/commits","review_comments_url":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360/comments","review_comment_url":"https://api.github.com/repos/thoughtbot/paperclip/pulls/comments{/number}","comments_url":"https://api.github.com/repos/thoughtbot/paperclip/issues/1360/comments","statuses_url":"https://api.github.com/repos/thoughtbot/paperclip/statuses/75926edf20ddadd82a4221ff366f432d3dc07d3a","head":{"label":"howaboutwe:generators-upstream","ref":"generators-upstream","sha":"75926edf20ddadd82a4221ff366f432d3dc07d3a","user":{"login":"howaboutwe","id":279545,"avatar_url":"https://avatars.githubusercontent.com/u/279545?v=3","gravatar_id":"","url":"https://api.github.com/users/howaboutwe","html_url":"https://github.com/howaboutwe","followers_url":"https://api.github.com/users/howaboutwe/followers","following_url":"https://api.github.com/users/howaboutwe/following{/other_user}","gists_url":"https://api.github.com/users/howaboutwe/gists{/gist_id}","starred_url":"https://api.github.com/users/howaboutwe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/howaboutwe/subscriptions","organizations_url":"https://api.github.com/users/howaboutwe/orgs","repos_url":"https://api.github.com/users/howaboutwe/repos","events_url":"https://api.github.com/users/howaboutwe/events{/privacy}","received_events_url":"https://api.github.com/users/howaboutwe/received_events","type":"Organization","site_admin":false},"repo":{"id":4702133,"name":"paperclip","full_name":"howaboutwe/paperclip","owner":{"login":"howaboutwe","id":279545,"avatar_url":"https://avatars.githubusercontent.com/u/279545?v=3","gravatar_id":"","url":"https://api.github.com/users/howaboutwe","html_url":"https://github.com/howaboutwe","followers_url":"https://api.github.com/users/howaboutwe/followers","following_url":"https://api.github.com/users/howaboutwe/following{/other_user}","gists_url":"https://api.github.com/users/howaboutwe/gists{/gist_id}","starred_url":"https://api.github.com/users/howaboutwe/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/howaboutwe/subscriptions","organizations_url":"https://api.github.com/users/howaboutwe/orgs","repos_url":"https://api.github.com/users/howaboutwe/repos","events_url":"https://api.github.com/users/howaboutwe/events{/privacy}","received_events_url":"https://api.github.com/users/howaboutwe/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/howaboutwe/paperclip","description":"Easy file attachment management for ActiveRecord","fork":true,"url":"https://api.github.com/repos/howaboutwe/paperclip","forks_url":"https://api.github.com/repos/howaboutwe/paperclip/forks","keys_url":"https://api.github.com/repos/howaboutwe/paperclip/keys{/key_id}","collaborators_url":"https://api.github.com/repos/howaboutwe/paperclip/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/howaboutwe/paperclip/teams","hooks_url":"https://api.github.com/repos/howaboutwe/paperclip/hooks","issue_events_url":"https://api.github.com/repos/howaboutwe/paperclip/issues/events{/number}","events_url":"https://api.github.com/repos/howaboutwe/paperclip/events","assignees_url":"https://api.github.com/repos/howaboutwe/paperclip/assignees{/user}","branches_url":"https://api.github.com/repos/howaboutwe/paperclip/branches{/branch}","tags_url":"https://api.github.com/repos/howaboutwe/paperclip/tags","blobs_url":"https://api.github.com/repos/howaboutwe/paperclip/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/howaboutwe/paperclip/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/howaboutwe/paperclip/git/refs{/sha}","trees_url":"https://api.github.com/repos/howaboutwe/paperclip/git/trees{/sha}","statuses_url":"https://api.github.com/repos/howaboutwe/paperclip/statuses/{sha}","languages_url":"https://api.github.com/repos/howaboutwe/paperclip/languages","stargazers_url":"https://api.github.com/repos/howaboutwe/paperclip/stargazers","contributors_url":"https://api.github.com/repos/howaboutwe/paperclip/contributors","subscribers_url":"https://api.github.com/repos/howaboutwe/paperclip/subscribers","subscription_url":"https://api.github.com/repos/howaboutwe/paperclip/subscription","commits_url":"https://api.github.com/repos/howaboutwe/paperclip/commits{/sha}","git_commits_url":"https://api.github.com/repos/howaboutwe/paperclip/git/commits{/sha}","comments_url":"https://api.github.com/repos/howaboutwe/paperclip/comments{/number}","issue_comment_url":"https://api.github.com/repos/howaboutwe/paperclip/issues/comments{/number}","contents_url":"https://api.github.com/repos/howaboutwe/paperclip/contents/{+path}","compare_url":"https://api.github.com/repos/howaboutwe/paperclip/compare/{base}...{head}","merges_url":"https://api.github.com/repos/howaboutwe/paperclip/merges","archive_url":"https://api.github.com/repos/howaboutwe/paperclip/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/howaboutwe/paperclip/downloads","issues_url":"https://api.github.com/repos/howaboutwe/paperclip/issues{/number}","pulls_url":"https://api.github.com/repos/howaboutwe/paperclip/pulls{/number}","milestones_url":"https://api.github.com/repos/howaboutwe/paperclip/milestones{/number}","notifications_url":"https://api.github.com/repos/howaboutwe/paperclip/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/howaboutwe/paperclip/labels{/name}","releases_url":"https://api.github.com/repos/howaboutwe/paperclip/releases{/id}","created_at":"2012-06-18T14:55:49Z","updated_at":"2014-03-07T16:35:31Z","pushed_at":"2015-05-31T03:39:18Z","git_url":"git://github.com/howaboutwe/paperclip.git","ssh_url":"git@github.com:howaboutwe/paperclip.git","clone_url":"https://github.com/howaboutwe/paperclip.git","svn_url":"https://github.com/howaboutwe/paperclip","homepage":"http://thoughtbot.com/community/","size":3571,"stargazers_count":1,"watchers_count":1,"language":"Ruby","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":1,"default_branch":"master"}},"base":{"label":"thoughtbot:master","ref":"master","sha":"14356d7eccb9175819508d243fb4a92681d11208","user":{"login":"thoughtbot","id":6183,"avatar_url":"https://avatars.githubusercontent.com/u/6183?v=3","gravatar_id":"","url":"https://api.github.com/users/thoughtbot","html_url":"https://github.com/thoughtbot","followers_url":"https://api.github.com/users/thoughtbot/followers","following_url":"https://api.github.com/users/thoughtbot/following{/other_user}","gists_url":"https://api.github.com/users/thoughtbot/gists{/gist_id}","starred_url":"https://api.github.com/users/thoughtbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thoughtbot/subscriptions","organizations_url":"https://api.github.com/users/thoughtbot/orgs","repos_url":"https://api.github.com/users/thoughtbot/repos","events_url":"https://api.github.com/users/thoughtbot/events{/privacy}","received_events_url":"https://api.github.com/users/thoughtbot/received_events","type":"Organization","site_admin":false},"repo":{"id":8393,"name":"paperclip","full_name":"thoughtbot/paperclip","owner":{"login":"thoughtbot","id":6183,"avatar_url":"https://avatars.githubusercontent.com/u/6183?v=3","gravatar_id":"","url":"https://api.github.com/users/thoughtbot","html_url":"https://github.com/thoughtbot","followers_url":"https://api.github.com/users/thoughtbot/followers","following_url":"https://api.github.com/users/thoughtbot/following{/other_user}","gists_url":"https://api.github.com/users/thoughtbot/gists{/gist_id}","starred_url":"https://api.github.com/users/thoughtbot/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thoughtbot/subscriptions","organizations_url":"https://api.github.com/users/thoughtbot/orgs","repos_url":"https://api.github.com/users/thoughtbot/repos","events_url":"https://api.github.com/users/thoughtbot/events{/privacy}","received_events_url":"https://api.github.com/users/thoughtbot/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/thoughtbot/paperclip","description":"Easy file attachment management for ActiveRecord","fork":false,"url":"https://api.github.com/repos/thoughtbot/paperclip","forks_url":"https://api.github.com/repos/thoughtbot/paperclip/forks","keys_url":"https://api.github.com/repos/thoughtbot/paperclip/keys{/key_id}","collaborators_url":"https://api.github.com/repos/thoughtbot/paperclip/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/thoughtbot/paperclip/teams","hooks_url":"https://api.github.com/repos/thoughtbot/paperclip/hooks","issue_events_url":"https://api.github.com/repos/thoughtbot/paperclip/issues/events{/number}","events_url":"https://api.github.com/repos/thoughtbot/paperclip/events","assignees_url":"https://api.github.com/repos/thoughtbot/paperclip/assignees{/user}","branches_url":"https://api.github.com/repos/thoughtbot/paperclip/branches{/branch}","tags_url":"https://api.github.com/repos/thoughtbot/paperclip/tags","blobs_url":"https://api.github.com/repos/thoughtbot/paperclip/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/thoughtbot/paperclip/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/thoughtbot/paperclip/git/refs{/sha}","trees_url":"https://api.github.com/repos/thoughtbot/paperclip/git/trees{/sha}","statuses_url":"https://api.github.com/repos/thoughtbot/paperclip/statuses/{sha}","languages_url":"https://api.github.com/repos/thoughtbot/paperclip/languages","stargazers_url":"https://api.github.com/repos/thoughtbot/paperclip/stargazers","contributors_url":"https://api.github.com/repos/thoughtbot/paperclip/contributors","subscribers_url":"https://api.github.com/repos/thoughtbot/paperclip/subscribers","subscription_url":"https://api.github.com/repos/thoughtbot/paperclip/subscription","commits_url":"https://api.github.com/repos/thoughtbot/paperclip/commits{/sha}","git_commits_url":"https://api.github.com/repos/thoughtbot/paperclip/git/commits{/sha}","comments_url":"https://api.github.com/repos/thoughtbot/paperclip/comments{/number}","issue_comment_url":"https://api.github.com/repos/thoughtbot/paperclip/issues/comments{/number}","contents_url":"https://api.github.com/repos/thoughtbot/paperclip/contents/{+path}","compare_url":"https://api.github.com/repos/thoughtbot/paperclip/compare/{base}...{head}","merges_url":"https://api.github.com/repos/thoughtbot/paperclip/merges","archive_url":"https://api.github.com/repos/thoughtbot/paperclip/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/thoughtbot/paperclip/downloads","issues_url":"https://api.github.com/repos/thoughtbot/paperclip/issues{/number}","pulls_url":"https://api.github.com/repos/thoughtbot/paperclip/pulls{/number}","milestones_url":"https://api.github.com/repos/thoughtbot/paperclip/milestones{/number}","notifications_url":"https://api.github.com/repos/thoughtbot/paperclip/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/thoughtbot/paperclip/labels{/name}","releases_url":"https://api.github.com/repos/thoughtbot/paperclip/releases{/id}","created_at":"2008-04-10T19:58:20Z","updated_at":"2015-05-30T23:22:20Z","pushed_at":"2015-05-31T03:39:19Z","git_url":"git://github.com/thoughtbot/paperclip.git","ssh_url":"git@github.com:thoughtbot/paperclip.git","clone_url":"https://github.com/thoughtbot/paperclip.git","svn_url":"https://github.com/thoughtbot/paperclip","homepage":"https://thoughtbot.com/open-source","size":10703,"stargazers_count":6975,"watchers_count":6975,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1519,"mirror_url":null,"open_issues_count":94,"forks":1519,"open_issues":94,"watchers":6975,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360"},"html":{"href":"https://github.com/thoughtbot/paperclip/pull/1360"},"issue":{"href":"https://api.github.com/repos/thoughtbot/paperclip/issues/1360"},"comments":{"href":"https://api.github.com/repos/thoughtbot/paperclip/issues/1360/comments"},"review_comments":{"href":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360/comments"},"review_comment":{"href":"https://api.github.com/repos/thoughtbot/paperclip/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/thoughtbot/paperclip/pulls/1360/commits"},"statuses":{"href":"https://api.github.com/repos/thoughtbot/paperclip/statuses/75926edf20ddadd82a4221ff366f432d3dc07d3a"}}}}