viewlet 0.0.1 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -143,7 +143,22 @@ To include list_section viewlet JS in your application add
143
143
 
144
144
  to your `application.js`
145
145
 
146
- ## Misc
146
+ ## Tips
147
+
148
+ * You do not have to provide a block to `viewlet`:
149
+
150
+ ```haml
151
+ = viewlet(:password_strength)
152
+ ```
153
+
154
+ * You can use hash syntax (and block syntax):
155
+
156
+ ```haml
157
+ = viewlet(:password_strength, :levels => %w(none weak good))
158
+
159
+ = viewlet(:password_strength, :levels => %w(none weak good)) do |ps|
160
+ - ps.levels %w(none weak good excellent) # overrides levels
161
+ ```
147
162
 
148
163
  * Let's say we decide to make our list_section viewlet use
149
164
  third-party list re-ordering library (e.g. `orderable-list.js`).
@@ -189,14 +204,31 @@ that could be used as HTML id.
189
204
  `class_name` option lets you set custom viewlet class:
190
205
 
191
206
  ```haml
192
- = viewlet(:list_section, :class_name => "CustomListSectionViewlet") do
207
+ = viewlet(:list_section, {}, :class_name => "CustomListSectionViewlet") do
193
208
  ...
194
209
  ```
195
210
 
196
- * You do not have to pass in block to `viewlet`:
211
+ * If you are using HAML you can use special syntax to output a viewlet:
197
212
 
198
213
  ```haml
199
- = viewlet(:password_strength)
214
+ %list_section_viewlet # viewlet name suffixed with '_viewlet'
215
+ heading "Group members"
216
+ empty_description "No members in this group"
217
+
218
+ collapse_button false
219
+ add_button do
220
+ = link_to "Invite Members", new_group_member_path(@group)
221
+
222
+ items @group.members
223
+
224
+ row_title do |member|
225
+ .name= member.name
226
+ .summary= member.summary
227
+
228
+ row_details do |member|
229
+ = render :partial => "some_other_partial", :locals => {:member => member}
230
+
231
+ %password_strength_viewlet{:levels => %w(none weak good)}
200
232
  ```
201
233
 
202
234
  ## Todo
@@ -0,0 +1,4 @@
1
+ - css_classes = [("active" if defined?(active)), ("progress-striped" if defined?(striped))]
2
+
3
+ .progress{:class => css_classes.join(" ")}
4
+ .bar{:percent => "#{width}%"}
@@ -0,0 +1,6 @@
1
+ %progress_bar_viewlet{:percent => 20, :stripped => true, :active => true}
2
+
3
+ %progress_bar_viewlet
4
+ percent 20
5
+ stripped
6
+ active
data/lib/viewlet/base.rb CHANGED
@@ -17,14 +17,14 @@ module Viewlet
17
17
 
18
18
  private
19
19
 
20
- def method_missing(method, *args, &block)
21
- is_write_op = if @variables[method].is_a?(Proc)
20
+ def method_missing(variable_name, *args, &block)
21
+ is_write_op = if @variables[variable_name].is_a?(Proc)
22
22
  block.present?
23
23
  else
24
24
  args.any? || block.present?
25
25
  end
26
26
 
27
- send("_#{is_write_op ? :write : :read}_variable", method, *args, &block)
27
+ send("_#{is_write_op ? :write : :read}_variable", variable_name, *args, &block)
28
28
  end
29
29
 
30
30
  def _read_variable(name, *args, &block)
@@ -0,0 +1,70 @@
1
+ Haml::Parser.class_eval do
2
+ # `%simple_viewlet` becomes `= viewlet(:simple)`
3
+ # `%complex_viewlet` becomes `= viewlet(:complex) do |viewlet_0|`
4
+ def push_with_viewlet_tag(node)
5
+ if node.value[:name].try(:end_with?, "_viewlet")
6
+ node = build_viewlet_node(node)
7
+ end
8
+ push_without_viewlet_tag(node)
9
+ end
10
+ alias_method_chain :push, :viewlet_tag
11
+
12
+ raise "Haml::Parser#close_script already defined" if private_method_defined?(:close_script)
13
+ def close_script(node)
14
+ viewlet_pop
15
+ end
16
+
17
+ # `prop "whatever"` becomes `- viewlet_0.prop "whatever"`
18
+ def plain_with_viewlet_parent_tag(text, escape_html = nil)
19
+ if viewlet_definition?
20
+ silent_script(" #{viewlet_peek}.#{text}")
21
+ else
22
+ plain_without_viewlet_parent_tag(text, escape_html)
23
+ end
24
+ end
25
+ alias_method_chain :plain, :viewlet_parent_tag
26
+
27
+ private
28
+
29
+ def build_viewlet_node(node)
30
+ name = node.value[:name].gsub(/_viewlet$/, "")
31
+ attrs = attributes_string(node.value[:attributes_hashes])
32
+ block = "do |#{viewlet_push}|" if block_opened?
33
+ script(" viewlet(:#{name} #{attrs}) #{block}")
34
+ end
35
+
36
+ def attributes_string(attributes_hashes)
37
+ if attributes_hashes.empty?
38
+ attributes_hashes = ""
39
+ elsif attributes_hashes.size == 1
40
+ attributes_hashes = ", #{attributes_hashes.first}"
41
+ else
42
+ attributes_hashes = ", (#{attributes_hashes.join(").merge(")})"
43
+ end
44
+ end
45
+
46
+ def viewlet_peek
47
+ viewlet_names.last.try(:first)
48
+ end
49
+
50
+ # Viewlet variables can only be configured
51
+ # on the next indentation level from viewlet call
52
+ def viewlet_definition?
53
+ if viewlet_names.last
54
+ @line.tabs == viewlet_names.last.last + 1
55
+ end
56
+ end
57
+
58
+ def viewlet_push
59
+ viewlet_names.push(["viewlet_#{viewlet_names.size}", @line.tabs])
60
+ viewlet_peek
61
+ end
62
+
63
+ def viewlet_pop
64
+ viewlet_names.pop
65
+ end
66
+
67
+ def viewlet_names
68
+ @viewlet_names ||= []
69
+ end
70
+ end
@@ -1,9 +1,13 @@
1
1
  module Viewlet
2
2
  module Helpers
3
- def viewlet(name, options={}, &block)
3
+ def viewlet(name, variables={}, options={}, &block)
4
4
  klass = options[:class_name].try(:constantize) || Base
5
5
  viewlet = klass.new(name, self)
6
6
 
7
+ variables.each do |name, value|
8
+ viewlet.send(name, value)
9
+ end
10
+
7
11
  case block.arity
8
12
  when 0 then block.call
9
13
  when 1 then block.call(viewlet)
@@ -1,9 +1,13 @@
1
1
  module Viewlet
2
2
  class Railtie < ::Rails::Railtie
3
- initializer "viewlets.view_helpers" do
3
+ initializer "viewlet.view_helpers" do
4
4
  ActionView::Base.send :include, Viewlet::Helpers
5
5
  end
6
6
 
7
+ config.before_configuration do
8
+ require "viewlet/haml" if defined?(Haml)
9
+ end
10
+
7
11
  config.to_prepare do |app|
8
12
  Rails.application.config.assets.paths << Rails.root.join("app", "viewlets")
9
13
  end
@@ -1,3 +1,3 @@
1
1
  module Viewlet
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: viewlet
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 25
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 3
10
+ version: 0.0.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dmitriy Kalinin
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2012-07-20 00:00:00 -07:00
18
+ date: 2012-07-24 00:00:00 -07:00
19
19
  default_executable:
20
20
  dependencies: []
21
21
 
@@ -34,11 +34,14 @@ files:
34
34
  - MIT.LICENSE
35
35
  - README.md
36
36
  - Rakefile
37
+ - examples/bootstrap_progress_bar/bootstrap_progress_bar.html.haml
38
+ - examples/bootstrap_progress_bar/usage.hml.haml
37
39
  - examples/bootstrap_tabs/bootstrap_tabs.html.haml
38
40
  - examples/bootstrap_tabs/plugin.js
39
41
  - examples/bootstrap_tabs/usage.html.haml
40
42
  - lib/viewlet.rb
41
43
  - lib/viewlet/base.rb
44
+ - lib/viewlet/haml.rb
42
45
  - lib/viewlet/helpers.rb
43
46
  - lib/viewlet/railtie.rb
44
47
  - lib/viewlet/template.rb