cosme 0.2.0 → 0.3.0

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: 1ea7bf8200b00c604ef7593e5cad55d5a4df6e7b
4
- data.tar.gz: 7aae69b53444354337a985cd20d906b277a757ac
3
+ metadata.gz: 97b71eafc5ffa79cf32ba7f829498ff4ab193997
4
+ data.tar.gz: d75b33f27be90723d601b2b19864b7fdceac03bd
5
5
  SHA512:
6
- metadata.gz: 021bbed18041bc6c1dd527aa7bec020fb1ca9659db1e19fd508d36078fd011e82341c9221beb0b4df90e2bbcd7368b6922a8efe602d672db28e8f6f4a82fe859
7
- data.tar.gz: 2e79d61a94af04efd840f05b119a16846053866a41601025e588d3e2e882a8997e07b0a3b89372c0680024604667c538084e44e90e2d14cfb43b66def553600c
6
+ metadata.gz: bed17b4d87b95f5e26ed0357ef71ffc773c6cd641b9a7484f1c292f7e828d47f97d43d97ad517f160b0284c3f10556765acbb49c63de83af7775c7c29efb112e
7
+ data.tar.gz: 835cd689ac2bf3db1bb1b87fb64973cfddba52bad33825bc3fb8bd25b8e8c39d439d5571f294a24f83a686f764857d22f42ca6e2d2621d56480559b080a244cb
data/README.md CHANGED
@@ -33,6 +33,26 @@ Or install it yourself as:
33
33
 
34
34
  2. Create a cosmetic file into `app/cosmetics`.
35
35
 
36
+ ## Using Cosme#define
37
+
38
+ * `:routes` - The option to apply a cosmetic only on a specific route.
39
+
40
+ * `:controller` - Controller path. eg: 'admin/users' when called in Admin::UsersController. if you not set this option, apply a cosmetic in all controllers.
41
+
42
+ * `:routes` - Action name. eg: 'index' when called in Admin::UsersController#index. if you not set this option, apply a cosmetic in all actions.
43
+
44
+ * `:target` - String of [jQuery Selectors](https://api.jquery.com/category/selectors/).
45
+
46
+ * `:action` - One of the following:
47
+
48
+ * `:after` - Inserts after all elements that match the supplied selector.
49
+
50
+ * `:before` - Inserts before all elements that match the supplied selector.
51
+
52
+ * `:replace` - Replaces all elements that match the supplied selector.
53
+
54
+ * `:render` - Argument for ActionView::Base#render.
55
+
36
56
  ## Example
37
57
 
38
58
  Inserts html to all elements of .example:
@@ -24,6 +24,6 @@ class Cosme
24
24
  when 'after'
25
25
  $target.after(content)
26
26
  when 'replace'
27
- $target.html(content)
27
+ $target.replaceWith(content)
28
28
  else
29
29
  false
data/lib/cosme/helpers.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  module Cosme
2
2
  module Helpers
3
- def cosmeticize
4
- Cosme.all.map do |cosmetic|
3
+ def cosmeticize(controller = nil)
4
+ Cosme.all_for(controller).map do |cosmetic|
5
5
  content = cosmetic[:render] ? render(cosmetic[:render]) : render
6
6
  content_tag(:div, nil, class: 'cosmetic', data: cosmetic.except(:render).merge(content: "#{content}"))
7
7
  end.join.html_safe
@@ -8,6 +8,8 @@ module Cosme
8
8
  end
9
9
 
10
10
  def call(env)
11
+ @env = env
12
+
11
13
  response = @app.call(env)
12
14
  return response unless Cosme.auto_cosmeticize?
13
15
 
@@ -28,7 +30,8 @@ module Cosme
28
30
  end
29
31
 
30
32
  def insert_cosmeticize_tag(html)
31
- html.sub(/<body[^>]*>/) { [$~, cosmeticize].join }
33
+ cosmeticizer = cosmeticize(controller)
34
+ html.sub(/<body[^>]*>/) { [$~, cosmeticizer].join }
32
35
  end
33
36
 
34
37
  def new_response(response, new_html)
@@ -56,8 +59,54 @@ module Cosme
56
59
 
57
60
  # Use in Cosme::Helpers#cosmeticize
58
61
  def render(options = {})
59
- renderer = ActionView::Base.new(ActionController::Base.view_paths)
60
- renderer.render options
62
+ _helpers = helpers
63
+ view_context = ActionView::Base.new(ActionController::Base.view_paths, assigns, controller)
64
+ view_context.class_eval { _helpers.each { |h| include h } }
65
+ view_context.render(options)
66
+ end
67
+
68
+ def controller
69
+ return unless @env
70
+ @env['action_controller.instance']
71
+ end
72
+
73
+ def assigns
74
+ return {} unless controller
75
+ controller.view_context.assigns
76
+ end
77
+
78
+ def helpers
79
+ [
80
+ controller.try(:_helpers),
81
+ Rails.application.routes.url_helpers,
82
+ engines_helpers
83
+ ].compact
84
+ end
85
+
86
+ def engines_helpers
87
+ wodule = Module.new
88
+
89
+ isolated_engine_instances.each do |instance|
90
+ name = instance.engine_name
91
+
92
+ wodule.class_eval do
93
+ define_method "_#{name}" do
94
+ instance.routes.url_helpers
95
+ end
96
+ end
97
+
98
+ wodule.class_eval(<<-RUBY, __FILE__, __LINE__ + 1)
99
+ def #{name}
100
+ @_#{name} ||= _#{name}
101
+ end
102
+ RUBY
103
+ end
104
+
105
+ wodule
106
+ end
107
+
108
+ def isolated_engine_instances
109
+ Rails::Engine.subclasses.map(&:instance).select(&:isolated?)
61
110
  end
62
111
  end
63
112
  end
data/lib/cosme/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cosme
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
data/lib/cosme.rb CHANGED
@@ -26,6 +26,14 @@ module Cosme
26
26
  @cosmetics.values
27
27
  end
28
28
 
29
+ def all_for(controller)
30
+ return all if controller.blank?
31
+
32
+ all.select do |cosmetic|
33
+ routes_match?(controller, cosmetic[:routes])
34
+ end
35
+ end
36
+
29
37
  def disable_auto_cosmeticize!
30
38
  @disable_auto_cosmeticize = true
31
39
  end
@@ -33,5 +41,32 @@ module Cosme
33
41
  def auto_cosmeticize?
34
42
  @disable_auto_cosmeticize ? false : true
35
43
  end
44
+
45
+ private
46
+
47
+ def routes_match?(controller, routes)
48
+ return true unless routes
49
+ return false unless routes_match_action?(controller, routes)
50
+ return false unless routes_match_controller?(controller, routes)
51
+ true
52
+ end
53
+
54
+ def routes_match_action?(controller, routes)
55
+ return true unless routes[:action]
56
+ if routes[:action].is_a? Array
57
+ routes[:action].map(&:to_s).include? controller.action_name
58
+ else
59
+ controller.action_name == routes[:action].to_s
60
+ end
61
+ end
62
+
63
+ def routes_match_controller?(controller, routes)
64
+ return true unless routes[:controller]
65
+ if routes[:controller].is_a? Array
66
+ routes[:controller].map(&:to_s).include? controller.controller_path
67
+ else
68
+ controller.controller_path == routes[:controller].to_s
69
+ end
70
+ end
36
71
  end
37
72
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cosme
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - YOSHIDA Hiroki
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-10-09 00:00:00.000000000 Z
11
+ date: 2015-10-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack