themeable 1.0.5 → 1.1.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: db4bc5a365caaf437b4570e522968895ec67a6ff
4
- data.tar.gz: 010b87b4f96cc921bd8bd9b9662f414b21dd72fa
3
+ metadata.gz: aa102ec9cc422bf2223a5c7402c3cc989cbc10c2
4
+ data.tar.gz: 3f9e3d3a8b5d83e79b9f70a7f8266c833760fae9
5
5
  SHA512:
6
- metadata.gz: f392d1c3542e60a2cc72181bc72db2fd003ffef6f2bfb8af21b3db2e39f280faf0717d02874362c63199c738305594175776947e7eada7a0dad411e49c6f9608
7
- data.tar.gz: 26019fbed804bba03dca79d1eae2444ec633db7c73f1a03206fa7f98668469c568d2b9a084c8643588ad8624b1697626d92c3684037c9d220cefe945c18d0080
6
+ metadata.gz: 3a4c0b7c41f364a13b8c85d1114380e20cbe1a123a76c8232d579b71c6690bc2b44b614d5de8d9ea6a1958b69c4f05e0035ec21e8781d63214cf960eb93ce0f9
7
+ data.tar.gz: 4fbe6abdd093f6bd0fde85d0a1390dfe2ade05ac9b238ff2158015431793aa97b25c5f34ec83b8e4155b76c668d105ee9ad6ca573f7ae2709e67a48dcfaa1e3b
data/README.md CHANGED
@@ -39,7 +39,7 @@ Start designing your theme in folder `theme`:
39
39
  └── application.html.erb
40
40
 
41
41
  - theme/assets/my_theme: where your stylesheets and javascripts should put in
42
- - theme/scaffold_templates/xxx: your scaffold templates, generated default and admin by default, sure you can add more
42
+ - theme/scaffold_templates/xxx: your scaffold templates, generated default and admin by default, of course you can add more if you want
43
43
  - theme/views: your views
44
44
 
45
45
 
@@ -92,12 +92,12 @@ Or, you can make this controller use dynamic themes:
92
92
  end
93
93
  end
94
94
 
95
- ### Use `theme_my_theme` as template engine to scaffold controller views
95
+ ### Use `theme_my_theme` as theme to scaffold controller views
96
96
 
97
- $ rails g scaffold_controller User --template-engine=theme_my_theme --scaffold-template=default
97
+ $ rails g scaffold_controller User --theme=my_theme
98
98
 
99
99
  create app/controllers/users_controller.rb
100
- invoke theme_my_theme
100
+ invoke erb
101
101
  create app/views/users
102
102
  create app/views/users/index.html.erb
103
103
  create app/views/users/edit.html.erb
@@ -114,6 +114,21 @@ Or, you can make this controller use dynamic themes:
114
114
  create app/views/users/show.json.jbuilder
115
115
  create app/views/users/_user.json.jbuilder
116
116
 
117
+ ### Choose scaffold template of theme
118
+
119
+ There are two templates automatically generated: default and admin
120
+
121
+ To choose default template, run:
122
+
123
+ `rails g scaffold_controller User --theme=my_theme --theme-scaffold=default`
124
+
125
+ To choose admin template, run:
126
+
127
+ `rails g scaffold_controller User --theme=my_theme --theme-scaffold=admin`
128
+
129
+ `--theme-scaffold=default` is default value if `--theme-scaffold` is not provided
130
+
131
+
117
132
  ### Customize `theme_my_theme` in your Rails project
118
133
 
119
134
  In your Rails project, if you feel `theme_my_theme` is not perfect, and want to make some changes to it only affect current Rails project, instead of all Rails projects which are using gem `theme_my_theme`, now you can do this:
@@ -125,35 +140,32 @@ In your Rails project, if you feel `theme_my_theme` is not perfect, and want to
125
140
  create app/assets/themes/my_theme/application.js
126
141
 
127
142
  $ rails g theme_my_theme:copy_views
128
- exist app/views
129
- identical app/views/layouts/.gitkeep
130
- conflict app/views/layouts/application.html.erb
131
- Overwrite /test_theme/app/views/layouts/application.html.erb? (enter "h" for help) [Ynaqdh] a
132
- force app/views/layouts/application.html.erb
133
- create lib/templates/theme_my_theme/scaffold
134
- create lib/templates/theme_my_theme/scaffold/admin/_form.html.erb
135
- create lib/templates/theme_my_theme/scaffold/admin/edit.html.erb
136
- create lib/templates/theme_my_theme/scaffold/admin/index.html.erb
137
- create lib/templates/theme_my_theme/scaffold/admin/new.html.erb
138
- create lib/templates/theme_my_theme/scaffold/admin/show.html.erb
139
- create lib/templates/theme_my_theme/scaffold/default/_form.html.erb
140
- create lib/templates/theme_my_theme/scaffold/default/edit.html.erb
141
- create lib/templates/theme_my_theme/scaffold/default/index.html.erb
142
- create lib/templates/theme_my_theme/scaffold/default/new.html.erb
143
- create lib/templates/theme_my_theme/scaffold/default/show.html.erb
144
-
145
- ### Set `theme_my_them` as default template engine
146
-
147
- If you feel each time to generate scaffold controller has to provide `--template-engine=theme_my_theme` is annoying, now you can set a default value like this:
148
-
149
- in config/application.rb, add `Themeable.template_engine = :my_theme`, note this is `:my_theme` but `:theme_my_theme`, this is different from the value of `--template-engine`
143
+ create app/themes/my_theme
144
+ create app/themes/my_theme/layouts/.gitkeep
145
+ create app/themes/my_theme/layouts/application.html.erb
146
+ create lib/templates/erb/scaffold/my_theme/admin/_form.html.erb
147
+ create lib/templates/erb/scaffold/my_theme/admin/edit.html.erb
148
+ create lib/templates/erb/scaffold/my_theme/admin/index.html.erb
149
+ create lib/templates/erb/scaffold/my_theme/admin/new.html.erb
150
+ create lib/templates/erb/scaffold/my_theme/admin/show.html.erb
151
+ create lib/templates/erb/scaffold/my_theme/default/_form.html.erb
152
+ create lib/templates/erb/scaffold/my_theme/default/edit.html.erb
153
+ create lib/templates/erb/scaffold/my_theme/default/index.html.erb
154
+ create lib/templates/erb/scaffold/my_theme/default/new.html.erb
155
+ create lib/templates/erb/scaffold/my_theme/default/show.html.erb
156
+
157
+ ### Set `theme_my_them` as default theme
158
+
159
+ If you feel each time to generate scaffold controller has to provide `--theme=my_theme` is annoying, now you can set a default value like this:
160
+
161
+ in config/application.rb, add `Themeable.default_theme = :my_theme`
150
162
 
151
163
  module TestTheme
152
164
  class Application < Rails::Application
153
165
 
154
166
  ....
155
167
 
156
- Themeable.template_engine = :my_theme
168
+ Themeable.default_theme = :my_theme
157
169
 
158
170
  ...
159
171
 
@@ -19,7 +19,9 @@ module Themeable
19
19
  view_paths = lookup_context.view_paths.to_a.map(&:to_path)
20
20
  theme = Themeable.theme(__themeable_theme_name)
21
21
  theme_view_path = File.join(theme.root_path, theme.theme_path, 'views')
22
- lookup_context.view_paths = view_paths.insert(1, theme_view_path)
22
+ view_paths.insert(1, theme_view_path)
23
+ view_paths.insert(0, Rails.root.join("app/themes/#{theme_name}"))
24
+ lookup_context.view_paths = view_paths
23
25
  end
24
26
  RUBY
25
27
  end
@@ -34,7 +34,6 @@ module Themeable
34
34
  # generators
35
35
  template 'generators/copy_views_generator.rb', "lib/generators/theme_#{theme_name}/copy_views_generator.rb"
36
36
  template 'generators/copy_assets_generator.rb', "lib/generators/theme_#{theme_name}/copy_assets_generator.rb"
37
- template 'generators/scaffold_generator.rb', "lib/generators/theme_#{theme_name}/scaffold_generator.rb"
38
37
 
39
38
  # rakes
40
39
  template 'resolve_css_path.rake', "lib/tasks/resolve_css_path.rake"
@@ -53,11 +52,11 @@ Dir.glob('lib/tasks/*.rake').each { |r| load r}
53
52
 
54
53
  # scaffold templates
55
54
  %w(default admin).each do |name|
56
- create_file "theme/scaffold_templates/#{name}/index.html.erb"
57
- create_file "theme/scaffold_templates/#{name}/edit.html.erb"
58
- create_file "theme/scaffold_templates/#{name}/show.html.erb"
59
- create_file "theme/scaffold_templates/#{name}/new.html.erb"
60
- create_file "theme/scaffold_templates/#{name}/_form.html.erb"
55
+ create_file "theme/scaffold_templates/#{theme_name}/#{name}/index.html.erb"
56
+ create_file "theme/scaffold_templates/#{theme_name}/#{name}/edit.html.erb"
57
+ create_file "theme/scaffold_templates/#{theme_name}/#{name}/show.html.erb"
58
+ create_file "theme/scaffold_templates/#{theme_name}/#{name}/new.html.erb"
59
+ create_file "theme/scaffold_templates/#{theme_name}/#{name}/_form.html.erb"
61
60
  end
62
61
 
63
62
  # vender files
@@ -0,0 +1,25 @@
1
+ module Themeable
2
+ module GeneratorConcern
3
+ def self.included(klass)
4
+ klass.instance_eval do
5
+ class_option :theme, type: :string, desc: "Choose theme to generate files", default: nil
6
+ class_option :theme_scaffold, type: :string, desc: "Choose a templete to scaffold", default: nil
7
+ end
8
+ end
9
+
10
+ # Support theme and template
11
+ def find_in_source_paths(file)
12
+ theme_scaffold = options[:theme_scaffold]
13
+ theme_scaffold ||= 'default' if options[:theme].present?
14
+ themed_file = [options[:theme], theme_scaffold, file].compact.join('/')
15
+ super(themed_file)
16
+ rescue Thor::Error => e
17
+ if e.message =~ /^Could not find /
18
+ say "Can't find themed file #{themed_file}, use default template", :yellow
19
+ return super(file)
20
+ else
21
+ raise
22
+ end
23
+ end
24
+ end
25
+ end
@@ -7,8 +7,9 @@ module Themeable
7
7
  end
8
8
 
9
9
  initializer :themeable do
10
- config.app_generators.template_engine Themeable.template_engine if Themeable.template_engine
10
+ config.app_generators.theme = Themeable.default_theme if Themeable.default_theme
11
11
  Themeable.themes.each do |theme|
12
+
12
13
  config.assets.paths << File.join(theme.root_path, theme.theme_path, 'assets')
13
14
  config.assets.paths << File.join(theme.root_path, 'vendor')
14
15
  config.assets.precompile << /\A#{Regexp.escape(theme.theme_name)}\/[^\/]+\.(js|css)\z/
@@ -17,7 +18,12 @@ module Themeable
17
18
  end
18
19
 
19
20
  generators do |app|
20
- Rails::Generators::ScaffoldControllerGenerator.class_option :scaffold_template, default: 'default', desc: 'Which scaffold template of themeable plugin you want to use'
21
+ require 'themeable/generator_concern'
22
+ require 'rails/generators/erb/scaffold/scaffold_generator'
23
+ Erb::Generators::ScaffoldGenerator.send :include, Themeable::GeneratorConcern
24
+ Themeable.themes.each do |theme|
25
+ Erb::Generators::ScaffoldGenerator.source_paths << File.join(theme.root_path, theme.theme_path, 'scaffold_templates')
26
+ end
21
27
  end
22
28
 
23
29
  end
@@ -11,7 +11,7 @@ module Themeable
11
11
 
12
12
  # set default values
13
13
  caller_file = caller.first
14
- if caller_file =~ %r{/theme_(.+?)/lib/theme_\1\.rb}
14
+ if caller_file =~ %r{/lib/theme_([^/]*)\.rb}
15
15
  default_theme_name = $1.to_sym
16
16
  default_root = File.expand_path(File.join(caller_file, '../../'))
17
17
  end
@@ -23,7 +23,7 @@ module Themeable
23
23
  #
24
24
  # @return [Symbol]
25
25
  def theme_name
26
- @theme_name || raise("Theme name is no defined")
26
+ @theme_name || raise("Theme name can't be resolved from path: #{__FILE__}")
27
27
  end
28
28
 
29
29
  @root_path = #{default_root.inspect}
@@ -42,32 +42,6 @@ module Themeable
42
42
  @theme_path || 'theme'
43
43
  end
44
44
 
45
- protected
46
-
47
- # Set theme name
48
- #
49
- # @param [String, Symbol] theme_name
50
- # @return [Symbol] symbolized theme_name
51
- def set_theme_name(theme_name)
52
- @theme_name = theme_name.to_sym
53
- end
54
-
55
- # Set root path of theme project
56
- #
57
- # @param [String] path
58
- # @return [String]
59
- def set_root_path(path)
60
- @root_path = path
61
- end
62
-
63
- # Set theme's relative path, which path includes assets and views
64
- #
65
- # @param [String] path is a relative path where assets and views locate.
66
- # @return [String]
67
- def set_theme_path(path)
68
- @theme_path = theme_path
69
- end
70
-
71
45
  RUBY
72
46
  end
73
47
  end
@@ -1,3 +1,3 @@
1
1
  module Themeable
2
- VERSION = "1.0.5"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/themeable.rb CHANGED
@@ -20,12 +20,12 @@ module Themeable
20
20
  themes.find{|t| t.theme_name == theme_name.to_sym } || raise("Theme #{theme_name} not found")
21
21
  end
22
22
 
23
- def template_engine=(theme_name)
24
- @template_engine = "theme_#{theme(theme_name).theme_name}"
23
+ def default_theme(theme_name)
24
+ @default_theme = "theme_#{theme(theme_name).theme_name}"
25
25
  end
26
26
 
27
- def template_engine
28
- @template_engine
27
+ def default_theme
28
+ @default_theme
29
29
  end
30
30
 
31
31
  end
@@ -3,8 +3,15 @@ module Theme<%= theme_name.camelize %>
3
3
  class CopyViewsGenerator < Rails::Generators::Base
4
4
  source_root File.join(Theme<%= theme_name.camelize %>.root_path, 'theme')
5
5
  def copy_views
6
- directory 'views', 'app/views'
7
- directory 'scaffold_templates', 'lib/templates/theme_<%= theme_name %>/scaffold'
6
+ directory 'views', 'app/themes/<%= theme_name %>'
7
+
8
+ root = self.class.source_root
9
+ template_engine = Rails.application.config.app_generators.rails[:template_engine] || 'erb'
10
+ Dir.glob(File.join(root, "scaffold_templates/<%= theme_name %>/**/*.#{template_engine}")) do |file|
11
+ source_path = Pathname.new(file).relative_path_from(Pathname.new(root))
12
+ destination_path = Pathname.new(file).relative_path_from(Pathname.new(File.join(root, 'scaffold_templates/<%= theme_name %>')))
13
+ copy_file source_path, "lib/templates/#{template_engine}/scaffold/<%= theme_name %>/#{destination_path}"
14
+ end
8
15
  end
9
16
  end
10
17
  end
@@ -3,7 +3,4 @@ require 'theme_<%= theme_name %>/version'
3
3
 
4
4
  module <%= app_name.camelize %>
5
5
  include Themeable::Theme
6
- # set_name '<%= theme_name %>' # by default, resovle name by file path automatically
7
- # set_root_path File.expand_path('../..', __FILE__)
8
- # set_theme_path 'theme'
9
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: themeable
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - xiaohui
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-10-10 00:00:00.000000000 Z
11
+ date: 2016-10-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -64,18 +64,17 @@ files:
64
64
  - README.md
65
65
  - Rakefile
66
66
  - bin/themeable
67
- - lib/generators/themeable/scaffold_admin_controller_generator.rb
68
67
  - lib/tasks/themeable_tasks.rake
69
68
  - lib/themeable.rb
70
69
  - lib/themeable/acts_as_themeable.rb
71
70
  - lib/themeable/command.rb
72
71
  - lib/themeable/css_path_resolver.rb
72
+ - lib/themeable/generator_concern.rb
73
73
  - lib/themeable/railtie.rb
74
74
  - lib/themeable/theme.rb
75
75
  - lib/themeable/version.rb
76
76
  - templates/generators/copy_assets_generator.rb
77
77
  - templates/generators/copy_views_generator.rb
78
- - templates/generators/scaffold_generator.rb
79
78
  - templates/resolve_css_path.rake
80
79
  - templates/theme_main.rb
81
80
  - templates/view_application.html.erb
@@ -1,17 +0,0 @@
1
- module Themeable
2
- module Generators
3
- class ScaffoldAdminControllerGenerator < Rails::Generators::NamedBase
4
-
5
- def do_everything
6
- return if @options.fetch('skip_scaffold_admin', false)
7
- old_invocations = {}.merge(@_invocations)
8
- @_invocations.clear
9
- new_options = @options.merge('skip_scaffold_admin' => true, 'theme_template' => 'admin')
10
- invoke :resource_route, ["admin/#{file_path}"]
11
- invoke :scaffold_controller, ["admin/#{file_path}"], new_options
12
- @_invocations.update(old_invocations)
13
- end
14
- end
15
- end
16
- end
17
-
@@ -1,30 +0,0 @@
1
- require 'rails/generators/erb/scaffold/scaffold_generator'
2
-
3
- module Theme<%= theme_name.camelize %>
4
- module Generators
5
- class ScaffoldGenerator < Erb::Generators::ScaffoldGenerator
6
- class_option :scaffold_template, type: :string, default: 'default',
7
- desc: "Choose template to scaffold: default or admin"
8
- source_root File.join(Theme<%= theme_name.camelize %>.root_path, "theme/scaffold_templates")
9
-
10
- def copy_view_files
11
- available_views.each do |view|
12
- filename = filename_with_extensions view
13
- template "#{options[:scaffold_template]}/#{view}.html.erb", File.join('app', 'views', controller_file_path, filename)
14
- end
15
- end
16
-
17
- protected
18
-
19
- def available_views
20
- ['index', 'edit', 'show', 'new', '_form']
21
- end
22
-
23
- def handler
24
- :erb
25
- end
26
-
27
- end
28
- end
29
- end
30
-