themeable 1.1.4 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: cef87560b607a92b5dfa8c4692ab57b6369eb859
4
- data.tar.gz: b9f2a3168a7c316731f249c7103bc3c440a91cc6
3
+ metadata.gz: 75b3c47db91293c2e49684d97d70f775791730d1
4
+ data.tar.gz: 232ff8ac46a535c430b5dbaa897415efd86d5e81
5
5
  SHA512:
6
- metadata.gz: 4c9a39cf63ab63e3dbd249d1435816e97b0642d332c6736edecd1c0a441dc76efa0c4b9f3c5deb754507ebda46543233503cdcaaa209f505ad499ee45bc0e6a2
7
- data.tar.gz: 562a6a08afb6aae83717cf390580fcf666da407360b1e5340883eabcf0db8fdf1f656d0e47f10579a6d0de4a7f418060a3268f0ee5b3a0b36dc7b2e50778993b
6
+ metadata.gz: e7df2f4254fe2cdf39cea6019b4f76e6f3496b0ff75061e005307f94828d225f05b4625bbc6fc39d2470d632a9a1b47ee67d4d90ea042466d8b4b23ca842f2df
7
+ data.tar.gz: a8438169271767e7f894cbf98e35653d2a8a0411fe9354517b80837d276a901bf3f65c7f7c022931ad8f980e48cb65ff0d7faf6b6013375c164871ec556a4148
data/README.md CHANGED
@@ -71,7 +71,7 @@ Some vendor files have relative urls in the CSS files for imports, images, etc.
71
71
  Add gem to Rails app:
72
72
 
73
73
  gem 'theme_my_theme', path: 'path_of_theme_my_theme'
74
-
74
+
75
75
  Use theme in controller:
76
76
 
77
77
  By default you need to remove `views/layouts/application.html.erb` from Rails app, make sure Rails can use theme's layout.
@@ -88,9 +88,9 @@ Or, you can make this controller use dynamic themes:
88
88
  acts_as_themeable :choose_theme
89
89
  def index
90
90
  end
91
-
91
+
92
92
  private
93
-
93
+
94
94
  def choose_theme
95
95
  case params[:theme]
96
96
  when 'my_theme' then 'my_theme'
@@ -103,7 +103,7 @@ Or, you can make this controller use dynamic themes:
103
103
  ### Use `theme_my_theme` as theme to scaffold controller views
104
104
 
105
105
  $ rails g scaffold_controller User --theme=my_theme
106
-
106
+
107
107
  create app/controllers/users_controller.rb
108
108
  invoke erb
109
109
  create app/views/users
@@ -142,11 +142,11 @@ To choose admin template, run:
142
142
  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:
143
143
 
144
144
  $ rails g theme_my_theme:copy_assets
145
-
145
+
146
146
  exist app/assets/themes
147
147
  create app/assets/themes/my_theme/application.css
148
148
  create app/assets/themes/my_theme/application.js
149
-
149
+
150
150
  $ rails g theme_my_theme:copy_views
151
151
  create app/themes/my_theme
152
152
  create app/themes/my_theme/layouts/.gitkeep
@@ -180,8 +180,30 @@ in config/application.rb, add `config.generators.theme = :my_theme`
180
180
  end
181
181
  end
182
182
 
183
+ ### Automatically choose theme scaffold template
184
+
185
+ Also, you can use a mapping to set `--theme` and `--theme-scaffold` automatically according to module name
186
+
187
+ module TestTheme
188
+ class Application < Rails::Application
189
+
190
+ ....
191
+
192
+ config.generators.theme_scaffold_mapping = {
193
+ # Admin::UsersController uses scaffold template from lib/templates/erb/scaffold/my_theme/admin
194
+ admin: { theme: 'my_theme', theme_scaffold: 'admin' },
195
+
196
+ # Agent::ProductsController uses scaffold template from lib/templates/erb/scaffold/my_theme/agent
197
+ agent: { theme: 'my_theme', theme_scaffold: 'agent' },
183
198
 
199
+ # Member::ProfileController uses scaffold template from lib/templates/erb/scaffold/other_theme/admin
200
+ member: { theme: 'other_theme', theme_scaffold: 'admin' },
184
201
 
202
+ # HomeController uses scaffold template from lib/templates/erb/scaffold/other_theme/default
203
+ default: { theme: 'other_theme', theme_scaffold: 'default' }
204
+ }
185
205
 
186
-
206
+ ...
187
207
 
208
+ end
209
+ end
@@ -7,11 +7,32 @@ module Themeable
7
7
  end
8
8
  end
9
9
 
10
+ def resolve_themeable_value(key)
11
+ return options[key] if options[key].present?
12
+ if theme_scaffold_mapping.size > 0
13
+ folders = name.split('/')
14
+ folders.pop
15
+ loop do
16
+ break if folders.size == 0
17
+ path = folders.join('/')
18
+ v = theme_scaffold_mapping.fetch(path.intern) { theme_scaffold_mapping.fetch(path.to_s, nil) }
19
+ vv = v && (v[key.intern] || v[key.to_s])
20
+ return vv if vv.present?
21
+ folders.pop
22
+ end
23
+ end
24
+ end
25
+
26
+ def theme_scaffold_mapping
27
+ Rails.application.config.generators.theme_scaffold_mapping
28
+ end
29
+
10
30
  # Support theme and template
11
31
  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('/')
32
+ theme = resolve_themeable_value(:theme)
33
+ theme_scaffold = resolve_themeable_value(:theme_scaffold)
34
+ theme_scaffold ||= 'default' if theme
35
+ themed_file = [theme, theme_scaffold, file].compact.join('/')
15
36
  super(themed_file)
16
37
  rescue Thor::Error => e
17
38
  if e.message =~ /^Could not find /
@@ -22,4 +43,4 @@ module Themeable
22
43
  end
23
44
  end
24
45
  end
25
- end
46
+ end
@@ -1,3 +1,3 @@
1
1
  module Themeable
2
- VERSION = "1.1.4"
2
+ VERSION = "1.2.0"
3
3
  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.1.4
4
+ version: 1.2.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-11-14 00:00:00.000000000 Z
11
+ date: 2016-11-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails