tao_on_rails 0.9.3 → 0.10.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: 1aec7845926da7ba72c3a2adc0375801cc91465b
4
- data.tar.gz: dc8dffdd444cf419667b54731aab0c59bfbe3144
3
+ metadata.gz: 9bafebffce03548b6276eaab7c9af6f5cf015fd3
4
+ data.tar.gz: 02f27e9957ae1e1387420e4f1d85ccbcdf5d40d5
5
5
  SHA512:
6
- metadata.gz: d6e2dc63d51c677dee8eacc042b725b45f91a286ee350115c6e8cd9dab83c293b113341f682b4715c7e7a4a36b78b1dbd7d38a507c031e0c8bc74c05e850e3c2
7
- data.tar.gz: f1b51e8276c9b98dd1df1ac5f95f1f98419bfac31e888da57515b00b7b453451364b54ddadea364d202b9aa1f6927f03a94310ee6ceb6f37d1429e827baaef19
6
+ metadata.gz: b3f076acc51ff5ae2f085332db2f13983824063ab03ecfa2fb4410324c80c764b6706b0bfda767cc3ca30ac0b7f21ac633e12e735d520c242b0691f3eaebbea2
7
+ data.tar.gz: 9d08e93ae5ef79aa4214c8ad0a9a4aedb2a5e2bbc236c67ed2870fbfa74f7e487576b72027cb98afe934e624c3fce4be639456d60f73705cd3a8247f862e58e2
data/README.md CHANGED
@@ -2,43 +2,14 @@
2
2
 
3
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.
4
4
 
5
- ## Usage
6
-
7
- Create new Tao on Rails application with following commands:
8
-
9
- ```bash
10
- rails new app_name -m https://git.io/vSvyw
11
- ```
12
-
13
- Several generators are available for you to quickly start your work:
14
-
15
- * tao:assets
16
- * tao:view
17
- * tao:controller
18
- * tao:component
19
- * tao:locale
20
- * tao:channel
21
- * tao:scaffold
22
-
23
- See `rails g tao:xxx --help` for more information.
24
-
25
- ## Plugin
26
-
27
- Start writing plugin for Tao on Rails with following commands:
28
-
29
- ```bash
30
- rails plugin new plugin_name -m https://git.io/vSvyy
31
- ```
32
-
33
5
  ## Documentation
34
6
 
35
- See [wiki](https://github.com/mycolorway/tao_on_rails/wiki) for tutorials and API reference.
7
+ See [tao.zhiren.com](https://tao.zhiren.com) for tutorials and API reference.
36
8
 
37
9
  ## Contributing
38
10
 
39
11
  Bug reports and pull requests are welcome on GitHub at https://github.com/mycolorway/tao_on_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
40
12
 
41
-
42
13
  ## License
43
14
 
44
15
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -38,7 +38,7 @@ class TaoAttributeManager extends TaoModule
38
38
  get: (element, name, options) ->
39
39
  value = parseFloat element.getAttribute(name)
40
40
  if _.isNaN value
41
- options.default || null
41
+ if _.isNumber(options.default) then options.default else null
42
42
  else
43
43
  value
44
44
  set: (element, name, val, options) ->
@@ -136,32 +136,15 @@ TaoComponentBasedOn = (superClassName = 'HTMLElement') ->
136
136
  @["_#{_.camelCase name}Changed"]?()
137
137
 
138
138
  reflow: ->
139
- @offsetHeight
139
+ Tao.helpers.reflow @
140
140
  @
141
141
 
142
142
  beforeCache: ->
143
143
  # called before turbolinks cache pages
144
144
 
145
145
  findComponent: (selectors...) ->
146
- readyCallback = selectors.pop() if _.isFunction(_.last(selectors))
147
- components = selectors.map (s) =>
148
- deferred = $.Deferred()
149
- element = @jq.find(s).get(0)
150
- if element.connected
151
- # make sure element is returned before callback
152
- setTimeout -> deferred.resolve()
153
- else
154
- @on 'tao:connected', s, (e) ->
155
- return unless e.target == e.currentTarget
156
- deferred.resolve()
157
- @off 'tao:connected', s
158
- [element, deferred.promise()]
159
-
160
- elements = components.map (c) -> c[0]
161
- promises = components.map (c) -> c[1]
162
- $.when(promises...).then -> readyCallback?(elements...)
163
-
164
- if elements.length > 1 then elements else elements[0]
146
+ callback = selectors.pop() if _.isFunction(_.last(selectors))
147
+ Tao.helpers.findComponent selectors, callback, @
165
148
 
166
149
  on: (name, args...) ->
167
150
  if name && name.indexOf('.') < 0
@@ -3,3 +3,32 @@ Tao.helpers =
3
3
 
4
4
  reflow: (el) ->
5
5
  $(el)[0].offsetHeight
6
+
7
+ findComponent: (selectors, callback, scope = document) ->
8
+ selectors = [selectors] unless _.isArray selectors
9
+ components = _.flatten selectors.map (s) => $(scope).find(s).get()
10
+
11
+ if components.length > 0 && _.isFunction callback
12
+ Tao.helpers.componentReady components, -> callback(components...)
13
+
14
+ if components.length > 1
15
+ components
16
+ else if components.length == 1
17
+ components[0]
18
+ else
19
+ null
20
+
21
+ componentReady: (components, callback) ->
22
+ components = [components] unless _.isArray components
23
+ promises = components.map (el) ->
24
+ deferred = $.Deferred()
25
+ if el.connected
26
+ setTimeout -> deferred.resolve()
27
+ else
28
+ el.on 'tao:connected.taoReady', (e) ->
29
+ return unless e.target == el
30
+ el.off 'tao:connected.taoReady'
31
+ deferred.resolve()
32
+ deferred.promise()
33
+
34
+ $.when(promises...).then -> callback()
@@ -5,8 +5,10 @@
5
5
  #= require lodash
6
6
 
7
7
  #= require_self
8
+ #= require ./helpers
9
+ #= require ./module
10
+ #= require ./component
8
11
  #= require ./application
9
12
  #= require ./page
10
- #= require ./helpers
11
13
 
12
14
  window.Tao = {}
@@ -0,0 +1,12 @@
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
@@ -0,0 +1,35 @@
1
+ module Tao
2
+ module Generators
3
+ class InstallGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('../templates', __FILE__)
5
+
6
+ def install_tao
7
+ assert_rails_version
8
+
9
+ gem 'tao_on_rails'
10
+
11
+ remove_file 'app/assets/javascripts/cable.js'
12
+ remove_file 'app/assets/javascripts/application.js'
13
+ remove_file 'app/assets/stylesheets/application.css'
14
+
15
+ template 'app/assets/javascripts/application.coffee'
16
+ template 'app/assets/stylesheets/application.scss'
17
+ template 'app/assets/stylesheets/_globals.scss'
18
+
19
+ template 'app/views/layouts/application.html.erb', force: true
20
+
21
+ template 'app/components/application_component.rb'
22
+ end
23
+
24
+ private
25
+
26
+ def assert_rails_version
27
+ requirement = Gem::Requirement.new('>= 5.0.0')
28
+ rails_version = Gem::Version.new(Rails::VERSION::STRING)
29
+ return if requirement.satisfied_by?(rails_version)
30
+ fail Rails::Generators::Error, 'Rails >= 5.0.0 is required'
31
+ end
32
+
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,12 @@
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()
@@ -0,0 +1 @@
1
+ // put global sass variables, mixins, functions here
@@ -0,0 +1,2 @@
1
+ //= require tao
2
+ //= require_tree ./
@@ -0,0 +1,7 @@
1
+ class ApplicationComponent < TaoOnRails::Components::Base
2
+
3
+ def self.tag_prefix
4
+ :app
5
+ end
6
+
7
+ end
@@ -0,0 +1,21 @@
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
+ </body>
21
+ </html>
data/lib/tao_on_rails.rb CHANGED
@@ -1,4 +1,3 @@
1
- require 'tao_on_rails/version'
2
1
  require 'tao_on_rails/engine'
3
2
 
4
3
  module TaoOnRails
@@ -1,6 +1,7 @@
1
1
  module TaoOnRails
2
2
  module ActionView
3
3
  module Helpers
4
+ extend ActiveSupport::Concern
4
5
 
5
6
  def page_id
6
7
  return @page_id if defined?(@page_id)
@@ -8,28 +9,31 @@ module TaoOnRails
8
9
  [controller_names, action_name].compact.flatten.join('_').dasherize
9
10
  end
10
11
 
11
- # Define the dynamic view helpers for components
12
- # This method should be called in action_view context
13
- def self.define_component_helpers
14
- load_tao_components
15
- ::ActiveSupport.run_load_hooks(:tao_components, self)
12
+ class_methods do
13
+ # Define the dynamic view helpers for components
14
+ # This method should be called in action_view context
15
+ def define_component_helpers
16
+ load_tao_components
17
+ ::ActiveSupport.run_load_hooks(:tao_components, self)
16
18
 
17
- TaoOnRails::Components::Base.descendants.each do |klass|
18
- module_eval %Q{
19
- def #{klass.tag_name.underscore} *args, &block
20
- #{klass.name}.new(self, *args).render(&block)
21
- end
22
- }
19
+ TaoOnRails::Components::Base.descendants.each do |klass|
20
+ module_eval %Q{
21
+ def #{klass.tag_name.underscore} *args, &block
22
+ #{klass.name}.new(self, *args).render(&block)
23
+ end
24
+ }
25
+ end
23
26
  end
24
- end
25
27
 
26
- def self.load_tao_components(root = Rails.root)
27
- Dir.glob([
28
- root.join('lib/components/**/*.rb'),
29
- root.join('app/components/**/*.rb')
30
- ]).each do |component|
31
- require_dependency component
28
+ def load_tao_components(root = Rails.root)
29
+ Dir.glob([
30
+ root.join('lib/**/components/**/*.rb'),
31
+ root.join('app/**/components/**/*.rb')
32
+ ]).each do |component|
33
+ require_dependency component
34
+ end
32
35
  end
36
+
33
37
  end
34
38
 
35
39
  end
@@ -4,12 +4,13 @@ module TaoOnRails
4
4
 
5
5
  attr_reader :options, :view
6
6
 
7
- delegate :tag_name, :component_name, :tag_prefix, :template_paths, :template_name, to: :class
7
+ delegate :component_name, :tag_prefix, :template_paths, :template_name, to: :class
8
8
 
9
9
  def initialize view, options = {}
10
10
  @view = view
11
11
  @options = merge_options default_options, options
12
- template_paths.unshift(@options.delete(:template_path)) if @options[:template_path].present?
12
+ template_paths.unshift(@options.delete(:template_path)) if @options.key?(:template_path)
13
+ @tag_name = @options.delete(:tag_name)
13
14
  end
14
15
 
15
16
  def render &block
@@ -47,6 +48,10 @@ module TaoOnRails
47
48
  @html_options ||= transform_html_options options
48
49
  end
49
50
 
51
+ def tag_name
52
+ @tag_name || self.class.tag_name
53
+ end
54
+
50
55
  def self.tag_name
51
56
  @tag_name ||= "#{self.tag_prefix}-#{self.component_name.to_s.dasherize}"
52
57
  end
@@ -1,5 +1,3 @@
1
- require 'tao_on_rails/components/base'
2
-
3
1
  module TaoOnRails
4
2
  module Components
5
3
  class PageComponent < Base
@@ -1,16 +1,17 @@
1
1
  require 'turbolinks'
2
2
  require 'jquery-rails'
3
3
  require 'lodash-rails'
4
- require 'tao_on_rails/action_view/helpers'
5
- require 'tao_on_rails/components'
6
4
 
7
5
  module TaoOnRails
8
6
  class Engine < Rails::Engine
9
7
 
10
- initializer "tao_on_rails.view_helpers" do |app|
8
+ config.eager_load_paths += Dir["#{config.root}/lib"]
9
+
10
+ initializer "tao_on_rails" do
11
11
  ::ActiveSupport.on_load :action_view do
12
- TaoOnRails::ActionView::Helpers.define_component_helpers
13
- include TaoOnRails::ActionView::Helpers
12
+ include ::TaoOnRails::ActionView::Helpers
13
+ load_tao_components ::TaoOnRails::Engine.root
14
+ define_component_helpers
14
15
  end
15
16
  end
16
17
 
@@ -1,3 +1,3 @@
1
1
  module TaoOnRails
2
- VERSION = "0.9.3"
2
+ VERSION = "0.10.0"
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: 0.9.3
4
+ version: 0.10.0
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: 2017-07-28 00:00:00.000000000 Z
12
+ date: 2017-09-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: turbolinks
@@ -181,6 +181,13 @@ files:
181
181
  - lib/generators/tao/controller/USAGE
182
182
  - lib/generators/tao/controller/controller_generator.rb
183
183
  - lib/generators/tao/controller/templates/controller.rb.erb
184
+ - lib/generators/tao/install/USAGE
185
+ - lib/generators/tao/install/install_generator.rb
186
+ - lib/generators/tao/install/templates/app/assets/javascripts/application.coffee
187
+ - lib/generators/tao/install/templates/app/assets/stylesheets/_globals.scss
188
+ - lib/generators/tao/install/templates/app/assets/stylesheets/application.scss
189
+ - lib/generators/tao/install/templates/app/components/application_component.rb
190
+ - lib/generators/tao/install/templates/app/views/layouts/application.html.erb
184
191
  - lib/generators/tao/locale/USAGE
185
192
  - lib/generators/tao/locale/locale_generator.rb
186
193
  - lib/generators/tao/locale/templates/model.yml.erb
@@ -200,7 +207,6 @@ files:
200
207
  - lib/generators/tao/view/view_generator.rb
201
208
  - lib/tao_on_rails.rb
202
209
  - lib/tao_on_rails/action_view/helpers.rb
203
- - lib/tao_on_rails/components.rb
204
210
  - lib/tao_on_rails/components/base.rb
205
211
  - lib/tao_on_rails/components/page_component.rb
206
212
  - lib/tao_on_rails/engine.rb
@@ -1 +0,0 @@
1
- require 'tao_on_rails/components/page_component'