korgi 0.1.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b9848cba04694aa95ef726d42866ad3891306361
4
- data.tar.gz: 1b633acca87fde4f2b64a4f7db9f057ac39ef8f9
3
+ metadata.gz: 050e5bb1bab1f7c286154c1c1100e0f5a8bcf268
4
+ data.tar.gz: dbe4c158ab61265306ac2d43342be42232534363
5
5
  SHA512:
6
- metadata.gz: 2a27d40f74e16d2f86274f67d6cef0763419649d5aaf713a7cc22d5fd3a34b8b977e02f8b8a8f802185656818fc63b709d465aef9d04be7553d5aad0b872d540
7
- data.tar.gz: 868a0a9c139f40063b4bcacb863675e9073f3f291cdb6dfbef8ea86a3036e7dd496f41a687178a1d99dd52d73b140cbe2968c575bbddfb2e1938f94fd718852f
6
+ metadata.gz: 272aabc24cb3f2de3815655a3c658e9c745c04bd8b16a2b7ee2c139669a9b10b5eedd083563eb7b1670115c2b4e93b0639b1459663da976b656fccf38bc60d9b
7
+ data.tar.gz: 18951b599032c7238fbaed538c85cdba45d29d7a1dada65ce9da3d43effb34a5cb961af4a69c386fd242d5de6c72118d63a9607fcc7ec794b56556dea34cc0c8
data/README.md CHANGED
@@ -1,6 +1,11 @@
1
1
  # Korgi
2
2
 
3
- [HTML::Pipeline](https://github.com/jch/html-pipeline) filters to generate urls for Rails resources with Markdown.
3
+ [![Gem Version](https://badge.fury.io/rb/korgi.svg)](https://badge.fury.io/rb/korgi)
4
+ [![Code Climate](https://codeclimate.com/github/jodeci/korgi/badges/gpa.svg)](https://codeclimate.com/github/jodeci/korgi)
5
+ [![Test Coverage](https://codeclimate.com/github/jodeci/korgi/badges/coverage.svg)](https://codeclimate.com/github/jodeci/korgi/coverage)
6
+ [![Build Status](https://travis-ci.org/jodeci/korgi.svg?branch=master)](https://travis-ci.org/jodeci/korgi)
7
+
8
+ [html-pipeline](https://github.com/jch/html-pipeline) filters to generate urls for Rails resources.
4
9
 
5
10
  ## Usage
6
11
 
@@ -11,7 +16,7 @@
11
16
  $+image.1$
12
17
 
13
18
  # filtered result
14
- uploads/image/file/1/thumb_pic.jpg
19
+ /uploads/image/file/1/thumb_pic.jpg
15
20
  ```
16
21
 
17
22
  *korgi* is meant to work with [CarrierWave](https://github.com/carrierwaveuploader/carrierwave). You will need to tell *korgi* how to map resources for you:
@@ -19,18 +24,22 @@ uploads/image/file/1/thumb_pic.jpg
19
24
  ```
20
25
  # config/initializers/korgi.rb
21
26
  Korgi.configure do |config|
22
- config.file_uploads = { image: [Image, :file, :thumb] }
27
+ config.file_uploads = { image: { model: Image, mount: :file, default_version: :thumb } }
23
28
  end
24
29
  ```
25
30
 
26
- This tells *korgi* that you have a CarrierWave uploader mounted to `Image:file`, with `:thumb` as it's default version. You can then simply write `$+image.1$` or `$+image.1.large$` in your markdown input, and *korgi* will replace the syntax with the associated url (and version).
31
+ This tells *korgi* that you have a CarrierWave uploader mounted to `Image` on `:file`, with `:thumb` as its default version. You can then simply write `$+image.1$` or `$+image.1.large$` in your markdown input, and *korgi* will replace the syntax with the associated url (and version).
27
32
 
28
33
  If you need the full url instead, you should change the settings for CarrierWave:
29
34
 
30
35
  ```
36
+ # config/initializers/carrierwave.rb
31
37
  CarrierWave.configure do |config|
32
38
  config.asset_host = "http://awesome.host.com"
33
39
  end
40
+
41
+ # filtered result
42
+ http://awesome.host.com/uploads/image/file/1/thumb_pic.jpg
34
43
  ```
35
44
 
36
45
  ### Korgi::NamedRouteFilter
@@ -40,7 +49,7 @@ end
40
49
  $#post.1$
41
50
 
42
51
  # filtered result
43
- /admin/posts/1
52
+ /posts/1
44
53
  ```
45
54
 
46
55
  You will need to tell *korgi* how to map resouces for you:
@@ -48,15 +57,24 @@ You will need to tell *korgi* how to map resouces for you:
48
57
  ```
49
58
  # config/initializers/korgi.rb
50
59
  Korgi.configure do |config|
51
- config.named_routes = { post: "admin/posts" }
60
+ config.named_routes = { post: { controller: :posts } }
61
+ end
62
+ ```
63
+
64
+ In Rails speak, this means that *korgi* will replace `$#post.1$` with the result of `post_path(id: 1)`. If you are using [FriendlyId](https://github.com/norman/friendly_id) to create url slugs, you can also do this:
65
+
66
+ ```
67
+ # config/initializers/korgi.rb
68
+ Korgi.configure do |config|
69
+ config.named_routes = { post: { controller: :posts, model: Post, primary_key: :slug } }
52
70
  end
53
71
  ```
54
72
 
55
- *korgi* will then be able to replace `$#post.1$` with the result of `admin_post_path(1)`.
73
+ This will enable *korgi* to interpret `$#post.1$` as `post_path(id: Post.find(1).slug)` instead, and will then return the slugged url, e,g, `/posts/slug-url`.
56
74
 
57
75
  ### putting it all together
58
76
 
59
- I tried to keep *korgi* simple, so you will still need to render the markdown syntax. Please do check out the documentation for [HTML::Pipeline](https://github.com/jch/html-pipeline). Here is an example snippet:
77
+ I tried to keep *korgi* simple, so it only generates the link, not the entire html. You will most likely be using *korgi* along Markdown, but I'll leave that decision to you. Please do check out the documentation for [html-pipeline](https://github.com/jch/html-pipeline) on how to render Markdown content. Here is an example snippet:
60
78
 
61
79
  ```
62
80
  pipeline =
@@ -67,11 +85,13 @@ pipeline =
67
85
  ]
68
86
  result = pipeline.call <<-EOD
69
87
  This is a [link to post1]($#post.1$).
88
+ Here is an [image]($+image.1$).
70
89
  EOD
71
90
  result[:output].to_s
72
91
 
73
92
  =>
74
- <p>This is a <a href="/admin/posts/1">link to post1</a>.</p>
93
+ <p>This is a <a href="/posts/1">link to post1</a>.</p>
94
+ <p>Here is an <img src="/uploads/image/file/1/thumb_pic.jpg" alt="image">.</p>
75
95
  ```
76
96
 
77
97
  ## Contributing
data/lib/korgi/config.rb CHANGED
@@ -3,7 +3,7 @@ require "active_support/configurable"
3
3
  module Korgi
4
4
  include ActiveSupport::Configurable
5
5
  config.instance_eval do
6
- self.named_routes = {} # { post: "dashboard/posts" }
7
- self.file_uploads = {} # { image: [Image, :file, :thumb] }
6
+ self.named_routes = {} # { post: { controller: :posts, model: Post, primary_key: :slug } }
7
+ self.file_uploads = {} # { image: { model: Image, mount: :file, default_version: :thumb, nil_object: NullImage } }
8
8
  end
9
9
  end
data/lib/korgi/engine.rb CHANGED
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require "rails/engine"
3
3
  require "carrierwave"
4
- require "mini_magick"
5
4
  module Korgi
6
5
  class Engine < ::Rails::Engine
7
6
  end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+ module Korgi
3
+ class FileObject
4
+ attr_reader :klass, :id, :nil_object
5
+
6
+ def initialize(klass, id, nil_object)
7
+ @klass = klass
8
+ @id = id
9
+ @nil_object = nil_object
10
+ end
11
+
12
+ def fetch
13
+ klass.find(id)
14
+ rescue ActiveRecord::RecordNotFound, NameError
15
+ nil_object || Korgi::NullFileObject
16
+ end
17
+ end
18
+
19
+ class NullFileObject
20
+ class << self
21
+ def url(_version)
22
+ nil
23
+ end
24
+ end
25
+ end
26
+ end
@@ -1,45 +1,70 @@
1
1
  # frozen_string_literal: true
2
2
  require "html/pipeline"
3
+ require "korgi/file_object"
3
4
  module Korgi
4
5
  class FileUploadFilter < ::HTML::Pipeline::Filter
6
+ attr_reader :target, :id, :version
7
+
5
8
  def initialize(doc, context = nil, result = nil)
6
9
  super doc, context, result
7
- @klass, @mount, @version = nil
10
+ @target, @id, @version = nil
8
11
  end
9
12
 
10
13
  def call
11
- doc.to_s.gsub(pattern) { replace(Regexp.last_match) }
14
+ doc.to_s.gsub(pattern) do
15
+ origin, @target, @id, @version = Regexp.last_match.to_a
16
+ valid_target? ? file_url : origin
17
+ end
12
18
  end
13
19
 
14
20
  private
15
21
 
16
- def replace(matches)
17
- result, model, id, version = matches.to_a
18
- @klass, @mount, @version = Korgi.config.file_uploads[model.to_sym]
19
- version ||= @version
20
- @klass.find(id).send(@mount).url(file_version(version))
21
- rescue ActiveRecord::RecordNotFound, NameError
22
- result
22
+ def pattern
23
+ %r{\$\+([\w]+).([\d]+)(?:.([\w]+))?\$}
24
+ end
25
+
26
+ def file_url
27
+ Korgi::FileObject.new(klass, id, nil_object).fetch.send(mount).url(file_version)
28
+ end
29
+
30
+ def valid_target?
31
+ Korgi.config.file_uploads.key?(target.to_sym)
32
+ end
33
+
34
+ def configured_value(key)
35
+ Korgi.config.file_uploads[target.to_sym][key]
36
+ end
37
+
38
+ def klass
39
+ configured_value(:model)
40
+ end
41
+
42
+ def mount
43
+ configured_value(:mount)
23
44
  end
24
45
 
25
- def file_version(version)
26
- if valid_file_version?(version)
27
- version
46
+ def nil_object
47
+ configured_value(:nil_object)
48
+ end
49
+
50
+ def default_version
51
+ configured_value(:default_version)
52
+ end
53
+
54
+ def file_version
55
+ if version
56
+ valid_file_version? ? version : default_version
28
57
  else
29
- @version
58
+ default_version
30
59
  end
31
60
  end
32
61
 
33
- def valid_file_version?(version)
62
+ def valid_file_version?
34
63
  file_versions.include?(version.to_sym)
35
64
  end
36
65
 
37
66
  def file_versions
38
- @klass.uploaders[@mount].versions.keys
39
- end
40
-
41
- def pattern
42
- %r{\$\+([\w]+).([\d]+)(?:.([\w]+))?\$}
67
+ klass.uploaders[mount].versions.keys
43
68
  end
44
69
  end
45
70
  end
@@ -1,13 +1,15 @@
1
1
  # frozen_string_literal: true
2
- require "rails/all"
3
2
  require "html/pipeline"
4
3
  module Korgi
5
4
  class NamedRouteFilter < ::HTML::Pipeline::Filter
5
+ attr_reader :target, :id
6
+
6
7
  include ActionView::Helpers
7
8
  include Rails.application.routes.url_helpers
8
9
 
9
10
  def initialize(doc, context = nil, result = nil)
10
11
  super doc, context, result
12
+ @target, @id = nil
11
13
  end
12
14
 
13
15
  def call
@@ -16,15 +18,45 @@ module Korgi
16
18
 
17
19
  private
18
20
 
21
+ def pattern
22
+ %r{\$#([\w]+).([\d]+)\$}
23
+ end
24
+
19
25
  def replace(matches)
20
- result, model, id = matches.to_a
21
- url_for(controller: Korgi.config.named_routes[model.to_sym], action: "show", id: id, only_path: true)
26
+ origin, @target, @id = matches.to_a
27
+ valid_target? ? resource_url : origin
22
28
  rescue ActionController::UrlGenerationError
23
- result
29
+ origin
24
30
  end
25
31
 
26
- def pattern
27
- %r{\$#([\w]+).([\d]+)\$}
32
+ def resource_url
33
+ url_for(controller: controller, action: "show", id: find_object, only_path: true)
34
+ end
35
+
36
+ def find_object
37
+ primary_key == :id ? id : klass.find(id).send(primary_key)
38
+ rescue ActiveRecord::RecordNotFound
39
+ id
40
+ end
41
+
42
+ def valid_target?
43
+ Korgi.config.named_routes.key?(target.to_sym)
44
+ end
45
+
46
+ def configured_value(key)
47
+ Korgi.config.named_routes[target.to_sym][key]
48
+ end
49
+
50
+ def controller
51
+ configured_value(:controller)
52
+ end
53
+
54
+ def klass
55
+ configured_value(:model)
56
+ end
57
+
58
+ def primary_key
59
+ configured_value(:primary_key) || :id
28
60
  end
29
61
  end
30
62
  end
data/lib/korgi/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
  module Korgi
3
- VERSION = "0.1.1"
3
+ VERSION = "0.2.2"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: korgi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tsehau Chao
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-02-04 00:00:00.000000000 Z
11
+ date: 2017-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -66,20 +66,6 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.0'
69
- - !ruby/object:Gem::Dependency
70
- name: mini_magick
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - "~>"
74
- - !ruby/object:Gem::Version
75
- version: '4.6'
76
- type: :runtime
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - "~>"
81
- - !ruby/object:Gem::Version
82
- version: '4.6'
83
69
  description: Extends the Markdown syntax to allow quick generation of links to Rails
84
70
  resources.
85
71
  email:
@@ -94,6 +80,7 @@ files:
94
80
  - lib/korgi.rb
95
81
  - lib/korgi/config.rb
96
82
  - lib/korgi/engine.rb
83
+ - lib/korgi/file_object.rb
97
84
  - lib/korgi/file_upload_filter.rb
98
85
  - lib/korgi/named_route_filter.rb
99
86
  - lib/korgi/railtie.rb