tao_on_rails 1.0.0.beta.1 → 2.0.0.pre.beta.1

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
- SHA1:
3
- metadata.gz: 5ba89a2aca102b14a98c51bcf0c5a57075cfdf34
4
- data.tar.gz: dbadc3a082ab9c79cb00aa6ada0a786c445a1eb8
2
+ SHA256:
3
+ metadata.gz: '0936b90ad1be05db3bc4a5c682d0145b1ccc4e5e43c3a2fb75e7837ee13cff0c'
4
+ data.tar.gz: 692a92c12c21146d1a25ee95b9b921b0964f4fb0f2d36540862395a74026706a
5
5
  SHA512:
6
- metadata.gz: e349016c0cf23a7877da2fb3215c346749f84483f50278a997eef1e0d9c5ccf8d68899813bf561fcd65faf88aef69cb69bd8819a7193ab3dce1713855d84f813
7
- data.tar.gz: d70070f3f4b1523e8ca62baf77b54475f81f962374d1451506708b6a6614d351684d8844a7d1ecb98bdcd7935fce4d05e443c6165fffaf80f16a25501c52fa53
6
+ metadata.gz: 112afbb785cb36242dba2ab4c2e9d085018824c891ef13984b9d6ac84ee84fa835d179824818901c6c26d31262d135e79ae38610a281624c28fa706c9eda355d
7
+ data.tar.gz: f3364403b5ca39a42ca533dfb2934a9fca7d1890fe16936f7712e2392e582ffe15c4c56a80ccd5d1b7daebef27a40d843a8ab5b0c8d378fe1b2be7c8841747bc
data/README.md CHANGED
@@ -1,10 +1,10 @@
1
1
  # Tao on Rails [![Build Status](https://travis-ci.org/mycolorway/tao_on_rails.svg?branch=master)](https://travis-ci.org/mycolorway/tao_on_rails)
2
2
 
3
- [Ruby on Rails](http://rubyonrails.org/) lacks a recommended way to structure your frontend code in large project for many years. Tao on Rails is the framework to fill the gap which will modularize your page with the new [Custom Elements v1](https://developers.google.com/web/fundamentals/getting-started/primers/customelements) API.
3
+ Tao on Rails provide both frontend and backend component solution for rails project.
4
4
 
5
- ## Documentation
5
+ ## Frontend Components
6
6
 
7
- See [tao.zhiren.com](https://tao.zhiren.com) for tutorials and API reference.
7
+ ## Backend Components
8
8
 
9
9
  ## Contributing
10
10
 
@@ -16,7 +16,7 @@ module TaoOnRails
16
16
  load_tao_components
17
17
  ::ActiveSupport.run_load_hooks(:tao_components, self)
18
18
 
19
- TaoOnRails::Components::Base.descendants.each do |klass|
19
+ TaoOnRails::BaseComponent.descendants.each do |klass|
20
20
  module_eval %Q{
21
21
  def #{klass.tag_name.underscore} *args, &block
22
22
  #{klass.name}.new(self, *args).render(&block)
@@ -0,0 +1,121 @@
1
+ module TaoOnRails
2
+ class BaseComponent
3
+
4
+ attr_reader :options, :view
5
+
6
+ delegate :component_name, :tag_prefix, :template_paths, :template_name, to: :class
7
+
8
+ def initialize view, options = {}
9
+ @view = view
10
+ @options = merge_options default_options, options
11
+ template_paths.unshift(@options.delete(:template_path)) if @options.key?(:template_path)
12
+ @tag_name = @options.delete(:tag_name)
13
+ end
14
+
15
+ def render &block
16
+ if template = find_template
17
+ render_template template, &block
18
+ else
19
+ view.content_tag tag_name, nil, html_options, &block
20
+ end
21
+ end
22
+
23
+ def render_template(template, &block)
24
+ if template.is_a?(String) || template.is_a?(Symbol)
25
+ template = find_template(template)
26
+ end
27
+
28
+ if template
29
+ if block_given?
30
+ block_content = view.capture(&block)
31
+ template.render(view, {component: self, block_given: true}) do |*name|
32
+ view._layout_for(*name) {block_content}
33
+ end
34
+ else
35
+ template.render(view, {component: self})
36
+ end
37
+ end
38
+ end
39
+
40
+ def html_options
41
+ @html_options ||= transform_html_options options
42
+ end
43
+
44
+ def tag_name
45
+ @tag_name || self.class.tag_name
46
+ end
47
+
48
+ def self.tag_name
49
+ @tag_name ||= "#{self.tag_prefix}-#{self.component_name.to_s.dasherize}"
50
+ end
51
+
52
+ def self.component_name
53
+ @component_name ||= self.name.underscore.split('/').map(&:singularize).join('_')
54
+ .gsub(/(.+)_component$/, '\1')
55
+ .gsub(/^#{Regexp.quote(self.tag_prefix.to_s.underscore)}_(.+)/, '\1')
56
+ end
57
+
58
+ def self.tag_prefix
59
+ :tao
60
+ end
61
+
62
+ def self.template_paths
63
+ @template_paths ||= [
64
+ "components/#{self.name.deconstantize.underscore}",
65
+ "#{self.name.deconstantize.underscore}/components"
66
+ ]
67
+ end
68
+
69
+ def self.template_name
70
+ @template_name ||= self.name.demodulize.underscore.gsub(/(.+)_component$/, '\1')
71
+ end
72
+
73
+ private
74
+
75
+ def default_options
76
+ {}
77
+ end
78
+
79
+ def merge_options options, other_options
80
+ options.merge(other_options) { |key, old_val, new_val|
81
+ if key.to_s == 'class'
82
+ old_val = old_val.split(' ') if old_val.is_a? String
83
+ new_val = new_val.split(' ') if new_val.is_a? String
84
+ Array(old_val) + Array(new_val)
85
+ elsif old_val.is_a?(Hash) && old_val.is_a?(Hash)
86
+ old_val.merge! new_val
87
+ else
88
+ new_val
89
+ end
90
+ }
91
+ end
92
+
93
+ def transform_html_options options, other_options = nil
94
+ if other_options
95
+ options = merge_options options, other_options
96
+ end
97
+
98
+ options.transform_keys { |key|
99
+ key.to_s.dasherize.to_sym
100
+ }.transform_values { |value|
101
+ case value
102
+ when true
103
+ ''
104
+ when false
105
+ nil
106
+ else
107
+ value
108
+ end
109
+ }
110
+ end
111
+
112
+ def find_template(name = template_name)
113
+ view.lookup_context.find_all(name, template_paths, true, template_keys, formats: [:html]).first
114
+ end
115
+
116
+ def template_keys
117
+ @template_keys ||= [:component, :block_given]
118
+ end
119
+
120
+ end
121
+ end
@@ -1,3 +1,3 @@
1
1
  module TaoOnRails
2
- VERSION = "1.0.0.beta.1"
2
+ VERSION = "2.0.0-beta.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tao_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0.beta.1
4
+ version: 2.0.0.pre.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Siyuan Liu
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-01-06 00:00:00.000000000 Z
12
+ date: 2019-01-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -59,9 +59,8 @@ dependencies:
59
59
  - - "~>"
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.2.1
62
- description: Ruby on Rails lacks a recommended way to structure your frontend code
63
- for many years. Tao on Rails is the framework to fill the gap which will modularize
64
- your page with the new Custom Elements v1 API.
62
+ description: Tao on Rails provide frontend component based on Custom Elements v1 and
63
+ backend component for rendering.
65
64
  email:
66
65
  - farthinker@mycolorway.com
67
66
  - t@mycolorway.com
@@ -72,20 +71,9 @@ files:
72
71
  - LICENSE
73
72
  - README.md
74
73
  - Rakefile
75
- - lib/generators/tao/component/USAGE
76
- - lib/generators/tao/component/component_generator.rb
77
- - lib/generators/tao/component/templates/component.rb.erb
78
- - lib/generators/tao/install/USAGE
79
- - lib/generators/tao/install/install_generator.rb
80
- - lib/generators/tao/install/templates/app/assets/javascripts/application.coffee
81
- - lib/generators/tao/install/templates/app/assets/stylesheets/_globals.scss
82
- - lib/generators/tao/install/templates/app/assets/stylesheets/application.scss
83
- - lib/generators/tao/install/templates/app/components/application_component.rb
84
- - lib/generators/tao/install/templates/app/views/layouts/application.html.erb
85
74
  - lib/tao_on_rails.rb
86
75
  - lib/tao_on_rails/action_view/helpers.rb
87
- - lib/tao_on_rails/components/base.rb
88
- - lib/tao_on_rails/components/page_component.rb
76
+ - lib/tao_on_rails/base_component.rb
89
77
  - lib/tao_on_rails/engine.rb
90
78
  - lib/tao_on_rails/version.rb
91
79
  homepage: https://github.com/mycolorway/tao_on_rails
@@ -107,9 +95,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
95
  - !ruby/object:Gem::Version
108
96
  version: 1.3.1
109
97
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.6.14
98
+ rubygems_version: 3.0.1
112
99
  signing_key:
113
100
  specification_version: 4
114
- summary: The missing frontend solution for Rails project
101
+ summary: The component solution for Rails project
115
102
  test_files: []
@@ -1,10 +0,0 @@
1
- Description:
2
- Generate controller files
3
-
4
- Example:
5
- `rails generate tao:component posts/autosave`
6
-
7
- Create file:
8
- app/components/posts/autosave_component.rb
9
- app/assets/javascripts/posts/components/autosave.coffee
10
- app/assets/stylesheets/posts/components/autosave.scss
@@ -1,20 +0,0 @@
1
- require 'rails/generators'
2
-
3
- module Tao
4
- module Generators
5
-
6
- class ComponentGenerator < Rails::Generators::NamedBase
7
- source_root File.expand_path('../templates', __FILE__)
8
-
9
- check_class_collision suffix: "Component"
10
-
11
- attr_reader :component_name
12
-
13
- def create_component_file
14
- @component_name = class_name.underscore.split('/').map(&:singularize).join('_')
15
- template "component.rb.erb", File.join('app/components', class_path, "#{file_name}_component.rb")
16
- end
17
-
18
- end
19
- end
20
- end
@@ -1,3 +0,0 @@
1
- class <%= class_name %>Component < ApplicationComponent
2
-
3
- end
@@ -1,12 +0,0 @@
1
- Description:
2
- install files of tao.
3
-
4
- Example:
5
- `rails generate tao:install`
6
-
7
- Create or update files:
8
- app/assets/javascripts/application.coffee
9
- app/assets/stylesheets/application.scss
10
- app/assets/stylesheets/_globals.scss
11
- app/views/layouts/application.html.erb
12
- app/components/application_component.rb
@@ -1,37 +0,0 @@
1
- require 'rails/generators'
2
-
3
- module Tao
4
- module Generators
5
- class InstallGenerator < Rails::Generators::Base
6
- source_root File.expand_path('../templates', __FILE__)
7
-
8
- def install_tao
9
- assert_rails_version
10
-
11
- gem 'tao_on_rails'
12
-
13
- # remove_file 'app/assets/javascripts/cable.js'
14
- # remove_file 'app/assets/javascripts/application.js'
15
- # remove_file 'app/assets/stylesheets/application.css'
16
- #
17
- # template 'app/assets/javascripts/application.coffee'
18
- # template 'app/assets/stylesheets/application.scss'
19
- # template 'app/assets/stylesheets/_globals.scss'
20
- #
21
- # template 'app/views/layouts/application.html.erb', force: true
22
-
23
- template 'app/components/application_component.rb'
24
- end
25
-
26
- private
27
-
28
- def assert_rails_version
29
- requirement = Gem::Requirement.new('>= 5.0.0')
30
- rails_version = Gem::Version.new(Rails::VERSION::STRING)
31
- return if requirement.satisfied_by?(rails_version)
32
- fail Rails::Generators::Error, 'Rails >= 5.0.0 is required'
33
- end
34
-
35
- end
36
- end
37
- end
@@ -1,12 +0,0 @@
1
- #= require tao
2
- #= require action_cable
3
- #= require_self
4
- #= require_tree .
5
-
6
- class Application extends TaoApplication
7
-
8
- _init: ->
9
- super
10
- @cable = ActionCable.createConsumer()
11
-
12
- window.app = new Application()
@@ -1 +0,0 @@
1
- // put global sass variables, mixins, functions here
@@ -1,2 +0,0 @@
1
- //= require tao
2
- //= require_tree ./
@@ -1,7 +0,0 @@
1
- class ApplicationComponent < TaoOnRails::Components::Base
2
-
3
- def self.tag_prefix
4
- :app
5
- end
6
-
7
- end
@@ -1,20 +0,0 @@
1
- <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8">
5
- <title><%%= content_for(:title) || 'A Tao on Rails Project' %></title>
6
- <%%= csrf_meta_tags %>
7
-
8
- <%%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
9
- <%%= content_for(:stylesheet) %>
10
-
11
- <%%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
12
- <%%= content_for(:javascript) %>
13
- </head>
14
-
15
- <body>
16
- <%%= tao_page layout: 'default' do %>
17
- <%%= yield %>
18
- <%% end %>
19
- </body>
20
- </html>
@@ -1,136 +0,0 @@
1
- module TaoOnRails
2
- module Components
3
- class Base
4
-
5
- attr_reader :options, :view
6
-
7
- delegate :component_name, :tag_prefix, :template_paths, :template_name, to: :class
8
-
9
- def initialize view, options = {}
10
- @view = view
11
- @options = merge_options default_options, options
12
- template_paths.unshift(@options.delete(:template_path)) if @options.key?(:template_path)
13
- @tag_name = @options.delete(:tag_name)
14
- end
15
-
16
- def render &block
17
- if template = find_template
18
- render_template template, &block
19
- else
20
- view.content_tag tag_name, nil, html_options, &block
21
- end
22
- end
23
-
24
- def render_template(template, &block)
25
- if template.is_a?(String) || template.is_a?(Symbol)
26
- template = find_template(template)
27
- end
28
-
29
- if template
30
- if block_given?
31
- block_content = view.capture(&block)
32
- template.render(view, {component: self, block_given: true}) do |*name|
33
- view._layout_for(*name) {block_content}
34
- end
35
- else
36
- template.render(view, {component: self})
37
- end
38
- end
39
- end
40
-
41
- def translate key, options = {}
42
- keys = [].tap do |result|
43
- component_class = self.class
44
- until component_class == TaoOnRails::Components::Base
45
- scope = component_class.name.underscore.split('/').join('.').gsub(/(.+)_component$/, '\1')
46
- result << "#{scope}.#{key}".to_sym
47
- component_class = component_class.superclass
48
- end
49
- end
50
- I18n.t(keys.shift, options.merge!(default: keys))
51
- end
52
- alias_method :t, :translate
53
-
54
- def html_options
55
- @html_options ||= transform_html_options options
56
- end
57
-
58
- def tag_name
59
- @tag_name || self.class.tag_name
60
- end
61
-
62
- def self.tag_name
63
- @tag_name ||= "#{self.tag_prefix}-#{self.component_name.to_s.dasherize}"
64
- end
65
-
66
- def self.component_name
67
- @component_name ||= self.name.underscore.split('/').map(&:singularize).join('_')
68
- .gsub(/(.+)_component$/, '\1')
69
- .gsub(/^#{Regexp.quote(self.tag_prefix.to_s.underscore)}_(.+)/, '\1')
70
- end
71
-
72
- def self.tag_prefix
73
- :tao
74
- end
75
-
76
- def self.template_paths
77
- @template_paths ||= [
78
- "components/#{self.name.deconstantize.underscore}",
79
- "#{self.name.deconstantize.underscore}/components"
80
- ]
81
- end
82
-
83
- def self.template_name
84
- @template_name ||= self.name.demodulize.underscore.gsub(/(.+)_component$/, '\1')
85
- end
86
-
87
- private
88
-
89
- def default_options
90
- {}
91
- end
92
-
93
- def merge_options options, other_options
94
- options.merge(other_options) { |key, old_val, new_val|
95
- if key.to_s == 'class'
96
- old_val = old_val.split(' ') if old_val.is_a? String
97
- new_val = new_val.split(' ') if new_val.is_a? String
98
- Array(old_val) + Array(new_val)
99
- elsif old_val.is_a?(Hash) && old_val.is_a?(Hash)
100
- old_val.merge! new_val
101
- else
102
- new_val
103
- end
104
- }
105
- end
106
-
107
- def transform_html_options options, other_options = nil
108
- if other_options
109
- options = merge_options options, other_options
110
- end
111
-
112
- options.transform_keys { |key|
113
- key.to_s.dasherize.to_sym
114
- }.transform_values { |value|
115
- case value
116
- when true
117
- ''
118
- when false
119
- nil
120
- else
121
- value
122
- end
123
- }
124
- end
125
-
126
- def find_template(name = template_name)
127
- view.lookup_context.find_all(name, template_paths, true, template_keys, formats: [:html]).first
128
- end
129
-
130
- def template_keys
131
- @template_keys ||= [:component, :block_given]
132
- end
133
-
134
- end
135
- end
136
- end
@@ -1,27 +0,0 @@
1
- module TaoOnRails
2
- module Components
3
- class PageComponent < Base
4
- attr_reader :page_id
5
-
6
- def initialize view, options = {}
7
- @page_id = view.page_id
8
- super
9
- end
10
-
11
- def render &block
12
- view.content_tag "#{view.page_id}-page", options, &block
13
- end
14
-
15
- def self.component_name
16
- :page
17
- end
18
-
19
- private
20
-
21
- def default_options
22
- {class: ['tao-page', "#{@page_id}-page"]}
23
- end
24
-
25
- end
26
- end
27
- end