cosme 0.3.0 → 0.4.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: 97b71eafc5ffa79cf32ba7f829498ff4ab193997
4
- data.tar.gz: d75b33f27be90723d601b2b19864b7fdceac03bd
3
+ metadata.gz: 922d8a8f714d52f5d8c888a06311b626cfb5e6ba
4
+ data.tar.gz: 1290c06f5169a29d3d2a4b32b64c1cad65c00ef0
5
5
  SHA512:
6
- metadata.gz: bed17b4d87b95f5e26ed0357ef71ffc773c6cd641b9a7484f1c292f7e828d47f97d43d97ad517f160b0284c3f10556765acbb49c63de83af7775c7c29efb112e
7
- data.tar.gz: 835cd689ac2bf3db1bb1b87fb64973cfddba52bad33825bc3fb8bd25b8e8c39d439d5571f294a24f83a686f764857d22f42ca6e2d2621d56480559b080a244cb
6
+ metadata.gz: 2c044f43cc7ed402aa7f55f797d8e919a733bf86d331d8d076737fb8180fe87938441775487cd86e658f696e30c6d536b4f5a229cc18fd199df5c5d75bc37eeb
7
+ data.tar.gz: 2a1c613511d5ef325e0281cd6de1c0a190ab3704295fe2e9ced8014b6a7f6cd282f893bc66cf1fcc615f0099e6f2f1bf50aa1bc0e87cbbac36b239018f2d6f4d
data/README.md CHANGED
@@ -25,21 +25,15 @@ Or install it yourself as:
25
25
 
26
26
  ## Usage
27
27
 
28
- 1. Require a script in your `app/assets/javascripts/application.coffee`:
28
+ Create a cosmetic file into `app/cosmetics`, and call `Cosme#define` method in the cosmetic file.
29
29
 
30
- ```coffee
31
- #= require cosme
32
- ```
33
-
34
- 2. Create a cosmetic file into `app/cosmetics`.
35
-
36
- ## Using Cosme#define
30
+ ### Cosme#define
37
31
 
38
32
  * `:routes` - The option to apply a cosmetic only on a specific route.
39
33
 
40
34
  * `:controller` - Controller path. eg: 'admin/users' when called in Admin::UsersController. if you not set this option, apply a cosmetic in all controllers.
41
35
 
42
- * `:routes` - Action name. eg: 'index' when called in Admin::UsersController#index. if you not set this option, apply a cosmetic in all actions.
36
+ * `:action` - Action name. eg: 'index' when called in Admin::UsersController#index. if you not set this option, apply a cosmetic in all actions.
43
37
 
44
38
  * `:target` - String of [jQuery Selectors](https://api.jquery.com/category/selectors/).
45
39
 
@@ -55,6 +49,8 @@ Or install it yourself as:
55
49
 
56
50
  ## Example
57
51
 
52
+ ### Cosmetic + View (2 files)
53
+
58
54
  Inserts html to all elements of .example:
59
55
 
60
56
  ```ruby
@@ -95,6 +91,46 @@ The result of the above:
95
91
  <h2>After Example</h2>
96
92
  ```
97
93
 
94
+ ### Cosmetic (1 file)
95
+
96
+ Replaces all elements of .example:
97
+
98
+ ```ruby
99
+ # app/cosmetics/replace_example.rb
100
+ Cosme.define(
101
+ target: '.example',
102
+ action: :replace
103
+ ).render(inline: <<-'SLIM', type: :slim)
104
+ .example
105
+ h1
106
+ | Replace Example
107
+ SLIM
108
+ ```
109
+
110
+ ```erb
111
+ <%# app/views/layouts/application.html.erb %>
112
+ <html>
113
+ <head>
114
+ <title>Example</title>
115
+ <%= stylesheet_link_tag 'application', media: 'all' %>
116
+ <%= javascript_include_tag 'application'%>
117
+ </head>
118
+ <body>
119
+ <div class="example">
120
+ <h1>Example</h1>
121
+ </div>
122
+ </body>
123
+ </html>
124
+ ```
125
+
126
+ The result of the above:
127
+
128
+ ```html
129
+ <div class="example">
130
+ <h1>Replace Example</h1>
131
+ </div>
132
+ ```
133
+
98
134
  ## Troubleshooting
99
135
 
100
136
  If the cosmetic does not work, please call `Cosme.disable_auto_cosmeticize!` and the folloing helper method in your view file.
@@ -103,6 +139,12 @@ If the cosmetic does not work, please call `Cosme.disable_auto_cosmeticize!` and
103
139
  <%= cosmeticize %>
104
140
  ```
105
141
 
142
+ And require a script in your `app/assets/javascripts/application.coffee`:
143
+
144
+ ```coffee
145
+ #= require cosme
146
+ ```
147
+
106
148
  ## Development
107
149
 
108
150
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -0,0 +1,14 @@
1
+ module Cosme
2
+ class CosmeticNotFound < StandardError; end
3
+
4
+ class Cosmetic
5
+ def initialize(cosmetic)
6
+ @cosmetic = cosmetic
7
+ end
8
+
9
+ def render(options)
10
+ raise Cosme::CosmeticNotFound, 'Cosmetic is not found. Please call `Cosme#define` method first.' unless @cosmetic
11
+ @cosmetic[:render] = options
12
+ end
13
+ end
14
+ end
data/lib/cosme/engine.rb CHANGED
@@ -13,5 +13,7 @@ module Cosme
13
13
  ActiveSupport.on_load(:action_view) do
14
14
  ActionView::Base.send(:include, Cosme::Helpers)
15
15
  end
16
+
17
+ config.assets.precompile += %w( cosme.js ) if config.respond_to? :assets
16
18
  end
17
19
  end
@@ -17,6 +17,7 @@ module Cosme
17
17
  return response unless html
18
18
 
19
19
  new_html = insert_cosmeticize_tag(html)
20
+ new_html = insert_cosme_js(new_html)
20
21
  new_response(response, new_html)
21
22
  end
22
23
 
@@ -34,6 +35,13 @@ module Cosme
34
35
  html.sub(/<body[^>]*>/) { [$~, cosmeticizer].join }
35
36
  end
36
37
 
38
+ def insert_cosme_js(html)
39
+ view_context = controller.try(:view_context)
40
+ return html unless view_context
41
+ script = view_context.javascript_include_tag('cosme', 'data-turbolinks-track' => true)
42
+ html.sub(/<\/head>/) { [script, $~].join }
43
+ end
44
+
37
45
  def new_response(response, new_html)
38
46
  status, headers, _ = response
39
47
  headers['Content-Length'] = new_html.bytesize.to_s
data/lib/cosme/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Cosme
2
- VERSION = '0.3.0'
2
+ VERSION = '0.4.0'
3
3
  end
data/lib/cosme.rb CHANGED
@@ -5,6 +5,7 @@ require 'cosme/version'
5
5
  require 'cosme/helpers'
6
6
  require 'cosme/middleware'
7
7
  require 'cosme/engine'
8
+ require 'cosme/cosmetic'
8
9
 
9
10
  module Cosme
10
11
  class << self
@@ -15,6 +16,8 @@ module Cosme
15
16
 
16
17
  @cosmetics ||= {}
17
18
  @cosmetics[caller_path] = cosmetic
19
+
20
+ Cosme::Cosmetic.new(cosmetic)
18
21
  end
19
22
 
20
23
  def default_file_path_for(caller_path)
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.3.0
4
+ version: 0.4.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-16 00:00:00.000000000 Z
11
+ date: 2015-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack
@@ -116,6 +116,7 @@ files:
116
116
  - gemfiles/action_pack_edge.gemfile
117
117
  - gemfiles/common.gemfile
118
118
  - lib/cosme.rb
119
+ - lib/cosme/cosmetic.rb
119
120
  - lib/cosme/engine.rb
120
121
  - lib/cosme/helpers.rb
121
122
  - lib/cosme/middleware.rb