themeable 1.0.3 → 1.0.4

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: 226dd22ad75dcbfb96aa75860a0c8cb733cedfec
4
- data.tar.gz: 9dc9079e67e956c5486543615891f0ab19cc6ffb
3
+ metadata.gz: 5c2c88465cfcd7a507c2f118801ad8b5f707e568
4
+ data.tar.gz: ec2e1826e7d404f8b3286fe9f1716255fe4e18b9
5
5
  SHA512:
6
- metadata.gz: 33dffd44dd810e344ed29ec70625221a950588f341da2054950aea59e9ec1094e966011bffe221e2c49be4db32b3524141a74d3d417ee110811e946c8fe73b2d
7
- data.tar.gz: d2ffc2a5423dacc228742ba9f8bea34548242aeba590bcc7efb93277a4ae6f71d1a1db5c4235cbc6e0b5592bf8715df387dfc5fb8120979d5a69af9f3d90e83c
6
+ metadata.gz: 9dbd7f95f31a7c182ba13214eb6658374ece3a4765922801d7efae104b876577ce5bfa98bf0ed05524646ea90d3f8931ed664b817329328de06a2cd04795c134
7
+ data.tar.gz: 98f9b7da433ffdd4e7ff6c97336e9433e4c8ef30f3676f7562ffea1ee20a60c9b39613fdf1f89b356e1994762310386c67ecd2b365a9d0baa7b81798f9b5a984
data/README.md CHANGED
@@ -6,38 +6,70 @@ You can create theme with this tool, make your Rails app choose theme dynamicall
6
6
 
7
7
  gem i themeable
8
8
 
9
- ### Create my theme with command line
9
+ ### Create your theme with command line
10
10
 
11
- themeable new famous
11
+ themeable new my_theme
12
12
 
13
- Now, directory `theme_famous` created with this tool.
13
+ Now, directory `theme_my_theme` created with this tool.
14
14
 
15
- ### Design my theme
15
+ ### Design your theme
16
16
 
17
17
  Start designing your theme in folder `theme`:
18
18
 
19
- ├── theme
20
- │   ├── assets
21
- │   │   └── famous
22
-    │   ├── application.css
23
-    │   ├── application.js
24
- │   │   └── vendor
25
- │   └── views
26
-    └── layouts
27
-    └── application.html.erb
19
+ theme
20
+ ├── assets
21
+ │   └── my_theme
22
+ │   ├── application.css
23
+ │   └── application.js
24
+ ├── scaffold_templates
25
+ │   ├── admin
26
+    │   ├── _form.html.erb
27
+    │   ├── edit.html.erb
28
+ │   │   ├── index.html.erb
29
+ │   │   ├── new.html.erb
30
+ │   │   └── show.html.erb
31
+ │   └── default
32
+ │   ├── _form.html.erb
33
+ │   ├── edit.html.erb
34
+ │   ├── index.html.erb
35
+ │   ├── new.html.erb
36
+ │   └── show.html.erb
37
+ └── views
38
+ └── layouts
39
+ └── application.html.erb
28
40
 
29
- ### Use my theme in my Rails project
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
43
+ - theme/views: your views
44
+
45
+
46
+ If you have some vendor scripts and stylesheets, please put them in vendor/my_theme:
47
+
48
+ vendor
49
+ └── my_theme
50
+
51
+ To require vendor files, do it as this:
52
+
53
+ # in some.js
54
+
55
+ //= require my_theme/xxx/xxx
56
+
57
+ # in some.css
58
+
59
+ *= require my_theme/xxx/xxx
60
+
61
+ ### Use your theme in your Rails project
30
62
 
31
63
  Add gem to Rails app:
32
64
 
33
- gem 'theme_famous', path: 'path_of_theme_famous'
65
+ gem 'theme_my_theme', path: 'path_of_theme_my_theme'
34
66
 
35
67
  Use theme in controller:
36
68
 
37
69
  By default you need to remove `views/layouts/application.html.erb` from Rails app, make sure Rails can use theme's layout.
38
70
 
39
71
  class HomeController < ApplicationController
40
- acts_as_themeable 'famous'
72
+ acts_as_themeable 'my_theme'
41
73
  def index
42
74
  end
43
75
  end
@@ -53,9 +85,82 @@ Or, you can make this controller use dynamic themes:
53
85
 
54
86
  def choose_theme
55
87
  case params[:theme]
56
- when 'famous' then 'famous'
88
+ when 'my_theme' then 'my_theme'
57
89
  when 'classic' then 'classic'
58
90
  else; :none # :none means don't use theme
59
91
  end
60
92
  end
61
- end
93
+ end
94
+
95
+ ### Use `theme_my_theme` as template engine to scaffold controller views
96
+
97
+ $ rails g scaffold_controller User --template-engine=theme_my_theme --scaffold-template=default
98
+
99
+ create app/controllers/users_controller.rb
100
+ invoke theme_my_theme
101
+ create app/views/users
102
+ create app/views/users/index.html.erb
103
+ create app/views/users/edit.html.erb
104
+ create app/views/users/show.html.erb
105
+ create app/views/users/new.html.erb
106
+ create app/views/users/_form.html.erb
107
+ invoke test_unit
108
+ create test/controllers/users_controller_test.rb
109
+ invoke helper
110
+ create app/helpers/users_helper.rb
111
+ invoke test_unit
112
+ invoke jbuilder
113
+ create app/views/users/index.json.jbuilder
114
+ create app/views/users/show.json.jbuilder
115
+ create app/views/users/_user.json.jbuilder
116
+
117
+ ### Customize `theme_my_theme` in your Rails project
118
+
119
+ 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:
120
+
121
+ $ rails g theme_my_theme:copy_assets
122
+
123
+ exist app/assets/themes
124
+ create app/assets/themes/my_theme/application.css
125
+ create app/assets/themes/my_theme/application.js
126
+
127
+ $ 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`
150
+
151
+ module TestTheme
152
+ class Application < Rails::Application
153
+
154
+ ....
155
+
156
+ Themeable.template_engine = :my_theme
157
+
158
+ ...
159
+
160
+ end
161
+ end
162
+
163
+
164
+
165
+
166
+
@@ -0,0 +1,17 @@
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
+
@@ -17,7 +17,7 @@ module Themeable
17
17
  @app_name = "theme_#{name}"
18
18
  say('Initializing theme project...', :green)
19
19
  # say("\"rails plugin new #{app_name} -T\"")
20
- system "rails plugin new #{app_name} -T"
20
+ system "rails plugin new #{app_name} -T -B"
21
21
 
22
22
  @destination_stack ||= []
23
23
  @destination_stack[0] = File.expand_path(app_name)
@@ -32,8 +32,9 @@ module Themeable
32
32
  end
33
33
 
34
34
  # generators
35
- template 'theme_views_generator.rb', "lib/generators/themeable/#{theme_name}/views_generator.rb"
36
- template 'theme_assets_generator.rb', "lib/generators/themeable/#{theme_name}/assets_generator.rb"
35
+ template 'generators/copy_views_generator.rb', "lib/generators/theme_#{theme_name}/copy_views_generator.rb"
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"
37
38
 
38
39
  # assets and views
39
40
  create_file "theme/assets/#{theme_name}/application.css"
@@ -41,6 +42,15 @@ module Themeable
41
42
  create_file "theme/views/layouts/.gitkeep"
42
43
  template "view_application.html.erb", "theme/views/layouts/application.html.erb"
43
44
 
45
+ # scaffold templates
46
+ %w(default admin).each do |name|
47
+ create_file "theme/scaffold_templates/#{name}/index.html.erb"
48
+ create_file "theme/scaffold_templates/#{name}/edit.html.erb"
49
+ create_file "theme/scaffold_templates/#{name}/show.html.erb"
50
+ create_file "theme/scaffold_templates/#{name}/new.html.erb"
51
+ create_file "theme/scaffold_templates/#{name}/_form.html.erb"
52
+ end
53
+
44
54
  # vender files
45
55
  create_file "vendor/#{theme_name}/.gitkeep"
46
56
 
@@ -1,16 +1,25 @@
1
1
  require 'rails'
2
2
  module Themeable
3
3
  class Railtie < Rails::Railtie
4
+
4
5
  config.to_prepare do
5
6
  ActionController::Base.send :include, Themeable::ActsAsThemeable
6
7
  end
8
+
7
9
  initializer :themeable do
10
+ config.app_generators.template_engine Themeable.template_engine if Themeable.template_engine
8
11
  Themeable.themes.each do |theme|
9
12
  config.assets.paths << File.join(theme.root_path, theme.theme_path, 'assets')
10
13
  config.assets.paths << File.join(theme.root_path, 'vendor')
11
- config.assets.precompile << /\A#{Regexp.escape(theme.name)}\/[^\/]+\.(js|css)\z/
12
- config.assets.precompile << /\A#{Regexp.escape(theme.name)}\/.+\.(gif|jpg|png|svg)\z/
14
+ config.assets.precompile << /\A#{Regexp.escape(theme.theme_name)}\/[^\/]+\.(js|css)\z/
15
+ config.assets.precompile << /\A#{Regexp.escape(theme.theme_name)}\/.+\.(gif|jpg|png|svg)\z/
13
16
  end
14
17
  end
18
+
19
+ 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
+ end
22
+
15
23
  end
16
24
  end
25
+
@@ -1,7 +1,7 @@
1
1
  module Themeable
2
2
 
3
3
  # Theme's basic module, automatically define methods
4
- # - name
4
+ # - theme_name
5
5
  # - root_path
6
6
  # - theme_path
7
7
  module Theme
@@ -12,18 +12,18 @@ module Themeable
12
12
  # set default values
13
13
  caller_file = caller.first
14
14
  if caller_file =~ %r{/theme_(.+?)/lib/theme_\1\.rb}
15
- default_name = $1.to_sym
15
+ default_theme_name = $1.to_sym
16
16
  default_root = File.expand_path(File.join(caller_file, '../../'))
17
17
  end
18
18
 
19
19
  subclass.instance_eval <<-RUBY, __FILE__, __LINE__ + 1
20
- @name = #{default_name.inspect}
20
+ @theme_name = #{default_theme_name.inspect}
21
21
 
22
22
  # Theme name
23
23
  #
24
24
  # @return [Symbol]
25
- def name
26
- @name || raise("Theme name is no defined")
25
+ def theme_name
26
+ @theme_name || raise("Theme name is no defined")
27
27
  end
28
28
 
29
29
  @root_path = #{default_root.inspect}
@@ -46,10 +46,10 @@ module Themeable
46
46
 
47
47
  # Set theme name
48
48
  #
49
- # @param [String] name
50
- # @return [Symbol] symbolized name
51
- def set_name(name)
52
- @name = name.to_sym
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
53
  end
54
54
 
55
55
  # Set root path of theme project
@@ -1,3 +1,3 @@
1
1
  module Themeable
2
- VERSION = "1.0.3"
2
+ VERSION = "1.0.4"
3
3
  end
data/lib/themeable.rb CHANGED
@@ -5,17 +5,27 @@ require 'themeable/acts_as_themeable'
5
5
 
6
6
  module Themeable
7
7
 
8
+ module_function
9
+
8
10
  @@theme_set = Set.new
9
- def self.add_theme(theme_class)
11
+ def add_theme(theme_class)
10
12
  @@theme_set << theme_class
11
13
  end
12
14
 
13
- def self.themes
15
+ def themes
14
16
  @@theme_set
15
17
  end
16
18
 
17
- def self.theme(theme_name)
18
- themes.find{|t| t.name == theme_name.to_sym } || raise("Theme #{theme_name} not found")
19
+ def theme(theme_name)
20
+ themes.find{|t| t.theme_name == theme_name.to_sym } || raise("Theme #{theme_name} not found")
21
+ end
22
+
23
+ def template_engine=(theme_name)
24
+ @template_engine = "theme_#{theme(theme_name).theme_name}"
25
+ end
26
+
27
+ def template_engine
28
+ @template_engine
19
29
  end
20
30
 
21
31
  end
@@ -0,0 +1,11 @@
1
+ module Theme<%= theme_name.camelize %>
2
+ module Generators
3
+ class CopyAssetsGenerator < Rails::Generators::Base
4
+ source_root File.join(Theme<%= theme_name.camelize %>.root_path, 'theme')
5
+ def copy_assets
6
+ directory 'assets', 'app/assets/themes'
7
+ end
8
+ end
9
+ end
10
+ end
11
+
@@ -0,0 +1,12 @@
1
+ module Theme<%= theme_name.camelize %>
2
+ module Generators
3
+ class CopyViewsGenerator < Rails::Generators::Base
4
+ source_root File.join(Theme<%= theme_name.camelize %>.root_path, 'theme')
5
+ def copy_views
6
+ directory 'views', 'app/views'
7
+ directory 'scaffold_templates', 'lib/templates/theme_<%= theme_name %>/scaffold'
8
+ end
9
+ end
10
+ end
11
+ end
12
+
@@ -0,0 +1,30 @@
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
+
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.3
4
+ version: 1.0.4
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-07 00:00:00.000000000 Z
11
+ date: 2016-10-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -64,6 +64,7 @@ files:
64
64
  - README.md
65
65
  - Rakefile
66
66
  - bin/themeable
67
+ - lib/generators/themeable/scaffold_admin_controller_generator.rb
67
68
  - lib/tasks/themeable_tasks.rake
68
69
  - lib/themeable.rb
69
70
  - lib/themeable/acts_as_themeable.rb
@@ -71,9 +72,10 @@ files:
71
72
  - lib/themeable/railtie.rb
72
73
  - lib/themeable/theme.rb
73
74
  - lib/themeable/version.rb
74
- - templates/theme_assets_generator.rb
75
+ - templates/generators/copy_assets_generator.rb
76
+ - templates/generators/copy_views_generator.rb
77
+ - templates/generators/scaffold_generator.rb
75
78
  - templates/theme_main.rb
76
- - templates/theme_views_generator.rb
77
79
  - templates/view_application.html.erb
78
80
  homepage: https://github.com/xiaohui-zhangxh/
79
81
  licenses:
@@ -1,10 +0,0 @@
1
- module Themeable
2
- module <%= theme_name.camelize %>
3
- class AssetsGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../../../../../theme", __FILE__)
5
- def copy_assets
6
- directory 'assets', 'app/assets/themes'
7
- end
8
- end
9
- end
10
- end
@@ -1,10 +0,0 @@
1
- module Themeable
2
- module <%= theme_name.camelize %>
3
- class ViewsGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../../../../../theme", __FILE__)
5
- def copy_views
6
- directory 'views', 'app/views'
7
- end
8
- end
9
- end
10
- end