breezy 0.1.3 → 0.1.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module Breezy
2
- VERSION = '0.1.3'
2
+ VERSION = '0.1.4'
3
3
  end
@@ -7,13 +7,6 @@ module BreezyTemplate
7
7
  found
8
8
  end
9
9
 
10
- def array!(collection = [], *attributes)
11
- original_search_path = @search_path
12
- super
13
- ensure
14
- @search_path = original_search_path
15
- end
16
-
17
10
  def _filter_by_path(search_path)
18
11
  if search_path.is_a? ::String
19
12
  return _filter_by_path(search_path.split('.'))
@@ -21,11 +14,26 @@ module BreezyTemplate
21
14
  @search_path = search_path
22
15
  end
23
16
 
17
+ def _mapping_element(element, options)
18
+ if @search_path && !@search_path.empty?
19
+ original_search_path = @search_path
20
+ @search_path = original_search_path[1..-1]
21
+ if @search_path.size == 0
22
+ @found = super
23
+ else
24
+ yield element
25
+ end
26
+
27
+ @search_path = original_search_path
28
+ else
29
+ super
30
+ end
31
+ end
32
+
24
33
  def _prepare_collection_for_map(collection)
25
34
  if @search_path && !@search_path.empty?
26
35
  id_name, id_val = @search_path.first.split('=')
27
36
 
28
- @search_path = @search_path[1..-1]
29
37
 
30
38
  if (defined? ::ActiveRecord) && collection.is_a?(::ActiveRecord::Relation)
31
39
  if id_val
@@ -46,7 +54,11 @@ module BreezyTemplate
46
54
  found = collection[index]
47
55
  end
48
56
 
49
- collection = [found]
57
+ if found
58
+ collection = [found]
59
+ else
60
+ collection = []
61
+ end
50
62
  end
51
63
  else
52
64
  super
@@ -54,21 +66,34 @@ module BreezyTemplate
54
66
  end
55
67
 
56
68
  def set!(key, value = BLANK, *args)
69
+ return if @found
57
70
  options = args.first || {}
58
71
  options = _normalize_options(options)
59
72
 
60
- if ::Kernel.block_given? && @search_path && !@search_path.empty?
73
+ if @search_path && !@search_path.empty?
61
74
  if key.to_s == @search_path.first
62
75
  original_search_path = @search_path
63
76
  @search_path = original_search_path[1..-1]
64
77
  if @search_path.size == 0
65
- @found = if _cache_options?(options)
66
- _cache(*options[:cache]) { _scope { yield self } }
78
+ if ::Kernel.block_given?
79
+ @found = if _cache_options?(options)
80
+ _cache(*options[:cache]) { _scope { yield self } }
81
+ else
82
+ _scope { yield self }
83
+ end
84
+ elsif _partial_options?(options)
85
+ super
67
86
  else
68
- _scope { yield self }
87
+ @found = value
69
88
  end
70
89
  else
71
- yield self
90
+ if ::Kernel.block_given?
91
+ yield self
92
+ elsif _partial_options?(options)
93
+ super
94
+ else
95
+ ::Kernel.raise 'can not'
96
+ end
72
97
  end
73
98
 
74
99
  @search_path = original_search_path
@@ -0,0 +1,61 @@
1
+ module Breezy
2
+ module Generators
3
+ class InstallGenerator < ::Rails::Generators::Base
4
+ desc <<-DESC
5
+ Description:
6
+ Copy breezy files to your application.
7
+ DESC
8
+ def self.source_root
9
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
10
+ end
11
+
12
+ def create_views_and_layouts
13
+ empty_directory 'app/assets/javascripts/views'
14
+ empty_directory 'app/assets/javascripts/layouts'
15
+ empty_directory 'app/assets/javascripts/components'
16
+ end
17
+
18
+ def copy_view_component
19
+ copy_file 'View.js.jsx', 'app/assets/javascripts/components/View.js.jsx'
20
+ copy_file 'boot.js', 'app/assets/javascripts/boot.js'
21
+ copy_file 'Default.js.jsx', 'app/assets/javascripts/layouts/default.js.jsx'
22
+ end
23
+
24
+ def append_js_requires
25
+ app_js = "app/assets/javascripts/application.js"
26
+
27
+ if File.readlines("#{Rails.root}/#{app_js}").grep(/require_tree/).any?
28
+ inject_into_file app_js, before: '//= require_tree .' do
29
+ "//= require breezy\n//= require boot\n//= require_tree ./layouts\n//= require_tree ./views\n//= require_tree ./components\n"
30
+ end
31
+ end
32
+ end
33
+
34
+ def append_entry_point
35
+ app_html = 'app/views/layouts/application.html.erb'
36
+ js_tag = <<-JS_TAG
37
+ <script type="text/javascript">
38
+ document.addEventListener('breezy:load', function(event){
39
+ var props = {
40
+ view: event.data.view,
41
+ data: event.data.data
42
+ }
43
+ ReactDOM.render(React.createElement(window.App.Components.View, props), document.getElementById('app'));
44
+ });
45
+
46
+ $(function(){ <%= breezy_snippet %> });
47
+
48
+ </script>
49
+ JS_TAG
50
+
51
+ inject_into_file app_html, before: '</head>' do
52
+ js_tag
53
+ end
54
+
55
+ inject_into_file app_html, after: '</head>' do
56
+ "\n<div id='app'></div>"
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,7 @@
1
+ App.Layouts.Default = function(view) {
2
+ return (
3
+ <div className='container'>
4
+ {view}
5
+ </div>
6
+ );
7
+ }
@@ -0,0 +1,7 @@
1
+ App.Components.View = React.createClass({
2
+ render: function() {
3
+ var view = App.Views[this.props.view].call(this, this.props.data);
4
+ var layout = App.Layouts[this.props.layout || 'Default'];
5
+ return layout(view);
6
+ }
7
+ });
@@ -0,0 +1,5 @@
1
+ window.App = {
2
+ Views: {},
3
+ Layouts: {},
4
+ Components:{}
5
+ }
@@ -0,0 +1,15 @@
1
+ # Example:
2
+ #
3
+ # json.notice notice
4
+ #
5
+ # json.posts do
6
+ # json.array! @posts do |post|
7
+ # json.id post.id
8
+ # json.title post.title
9
+ # json.body post.body
10
+ #
11
+ # json.show_url post_path(post)
12
+ # json.edit_url edit_post_path(post)
13
+ # json.delete_url post_path(post)
14
+ # end
15
+ # end
@@ -0,0 +1,8 @@
1
+ App.Views.<%= @js_filename %> = function(json) {
2
+
3
+ return (
4
+ <div>
5
+ </div>
6
+ );
7
+ };
8
+
@@ -0,0 +1,35 @@
1
+ module Breezy
2
+ module Generators
3
+ class ViewGenerator < ::Rails::Generators::NamedBase
4
+ desc <<-DESC
5
+ Description:
6
+ Creates a content view and jsx view.
7
+ DESC
8
+ argument :actions, type: :array, default: [], banner: "action action"
9
+
10
+ def self.source_root
11
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
12
+ end
13
+
14
+ def copy_view_files
15
+ base_parts = class_path + [file_name]
16
+ content_destination = File.join("app/views", base_parts)
17
+ view_destination = "app/assets/javascripts/views"
18
+
19
+ empty_directory content_destination
20
+ empty_directory view_destination
21
+
22
+ actions.each do |action|
23
+ @action = action
24
+ @js_filename = (base_parts + [action]).map(&:camelcase).join
25
+ @content_path = File.join(content_destination, "#{@action}.js.breezy")
26
+ @view_path = File.join(view_destination, "#{@js_filename}.js.jsx")
27
+
28
+ template 'View.js.jsx', @view_path
29
+ template 'view.js.breezy', @content_path
30
+ end
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -3,6 +3,7 @@ require "mocha/setup"
3
3
  require "action_view"
4
4
  require "action_view/testing/resolvers"
5
5
  require "breezy_template"
6
+ require 'byebug'
6
7
 
7
8
  BLOG_POST_PARTIAL = <<-JBUILDER
8
9
  json.extract! blog_post, :id, :body
@@ -750,6 +751,49 @@ class BreezyTemplateTest < ActionView::TestCase
750
751
  assert_equal expected, result
751
752
  end
752
753
 
754
+ test "filtering for a raw value is also possble" do
755
+ result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
756
+ json.hit do
757
+ json.hit2 23
758
+ end
759
+
760
+ json.miss do
761
+ json.miss2 do
762
+ raise 'this should not be called'
763
+ json.greeting 'missed call'
764
+ end
765
+ end
766
+ JBUILDER
767
+ Rails.cache.clear
768
+
769
+ expected = strip_format(<<-JS)
770
+ (function(){
771
+ return (
772
+ {"data":23,"action":"graft","path":"hit.hit2"}
773
+ );
774
+ })()
775
+ JS
776
+
777
+ assert_equal expected, result
778
+ end
779
+
780
+ test "filter with partial" do
781
+ result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2.terms')
782
+ json.hit do
783
+ json.hit2 partial: "footer"
784
+ end
785
+ JBUILDER
786
+
787
+ expected = strip_format(<<-JS)
788
+ (function(){
789
+ return (
790
+ {"data":"You agree","action":"graft","path":"hit.hit2.terms"}
791
+ );
792
+ })()
793
+ JS
794
+ assert_equal expected, result
795
+ end
796
+
753
797
  test "filtering for a node in the tree via breezy_filter helper" do
754
798
  result = jbuild(<<-JBUILDER, breezy_filter: 'hit.hit2')
755
799
  json.hit do
@@ -824,7 +868,7 @@ class BreezyTemplateTest < ActionView::TestCase
824
868
  expected = strip_format(<<-JS)
825
869
  (function(){
826
870
  return (
827
- {"data":[{"title":"first"}],"action":"graft","path":"hit.hit2.id=1"}
871
+ {"data":{"title":"first"},"action":"graft","path":"hit.hit2.id=1"}
828
872
  );
829
873
  })()
830
874
  JS
@@ -856,7 +900,7 @@ class BreezyTemplateTest < ActionView::TestCase
856
900
  expected = strip_format(<<-JS)
857
901
  (function(){
858
902
  return (
859
- {"data":[{"title":"first"}],"action":"graft","path":"hit.hit2.0"}
903
+ {"data":{"title":"first"},"action":"graft","path":"hit.hit2.0"}
860
904
  );
861
905
  })()
862
906
  JS
@@ -879,7 +923,7 @@ class BreezyTemplateTest < ActionView::TestCase
879
923
  expected = strip_format(<<-JS)
880
924
  (function(){
881
925
  return (
882
- {"data":[{"name":"hit"}],"action":"graft","path":"hit.hit2.id=1"}
926
+ {"data":{"name":"hit"},"action":"graft","path":"hit.hit2.id=1"}
883
927
  );
884
928
  })()
885
929
  JS
@@ -903,7 +947,7 @@ class BreezyTemplateTest < ActionView::TestCase
903
947
  expected = strip_format(<<-JS)
904
948
  (function(){
905
949
  return (
906
- {"data":[{"name":"hit"}],"action":"graft","path":"hit.hit2.0"}
950
+ {"data":{"name":"hit"},"action":"graft","path":"hit.hit2.0"}
907
951
  );
908
952
  })()
909
953
  JS
@@ -1021,7 +1065,7 @@ class BreezyTemplateTest < ActionView::TestCase
1021
1065
 
1022
1066
  expected = strip_format(<<-JS)
1023
1067
  (function(){
1024
- return ({"data":{"world":32},"action":"graft","path":"hello.world"});
1068
+ return ({"data":32,"action":"graft","path":"hello.world"});
1025
1069
  })()
1026
1070
  JS
1027
1071
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: breezy
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Johny Ho
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-20 00:00:00.000000000 Z
11
+ date: 2017-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: coffee-rails
@@ -113,7 +113,18 @@ extensions: []
113
113
  extra_rdoc_files: []
114
114
  files:
115
115
  - README.md
116
+ - app/views/breezy/response.html.erb
116
117
  - lib/assets/javascripts/breezy.coffee
118
+ - lib/assets/javascripts/breezy/component_url.coffee
119
+ - lib/assets/javascripts/breezy/controller.coffee
120
+ - lib/assets/javascripts/breezy/csrf_token.coffee
121
+ - lib/assets/javascripts/breezy/doubly_linked_list.coffee
122
+ - lib/assets/javascripts/breezy/parallel_queue.coffee
123
+ - lib/assets/javascripts/breezy/progress_bar.coffee
124
+ - lib/assets/javascripts/breezy/remote.coffee
125
+ - lib/assets/javascripts/breezy/snapshot.coffee
126
+ - lib/assets/javascripts/breezy/start.coffee
127
+ - lib/assets/javascripts/breezy/utils.coffee
117
128
  - lib/breezy.rb
118
129
  - lib/breezy/active_support.rb
119
130
  - lib/breezy/configuration.rb
@@ -132,6 +143,13 @@ files:
132
143
  - lib/breezy_template/handler.rb
133
144
  - lib/breezy_template/partial_extension.rb
134
145
  - lib/breezy_template/search_extension.rb
146
+ - lib/generators/breezy/install/install_generator.rb
147
+ - lib/generators/breezy/install/templates/Default.js.jsx
148
+ - lib/generators/breezy/install/templates/View.js.jsx
149
+ - lib/generators/breezy/install/templates/boot.js
150
+ - lib/generators/breezy/view/templates/view.js.breezy
151
+ - lib/generators/breezy/view/templates/view.js.jsx
152
+ - lib/generators/breezy/view/view_generator.rb
135
153
  - test/blade_helper.rb
136
154
  - test/breezy_template_test.rb
137
155
  - test/breezy_test.rb