Skip to content

Commit 6f963ad

Browse files
committed
Add a generator to copy views from the engine into the host app
Fixes #64
1 parent 36562dd commit 6f963ad

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

README.md

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,19 @@ Many features of OCW can be enabled or disabled to meet your event's needs. Thes
8181

8282
### Views and Styles
8383

84-
You can override any view in OpenConferenceWare by creating a view with the same name in your host application. For example, replace the view showing a single room, you would create `app/views/open_conference_ware/rooms/show.html.erb` within your host application.
84+
Since OpenConferenceWare is an engine, all of its views are packaged inside the gem. To customize things, you can easily override any view by creating one with the same name in your application.
85+
86+
To simplify this process, we've included a generator that will copy views out of the gem for you. Invoking the following command will copy all of OpenConferenceWare's views:
87+
88+
$ bin/rails generate open_conference_ware:views
89+
90+
If you only need to override a particular set of views, you can pass arguments to the generator to narrow things down. This will copy only the layout file and view related to rooms:
91+
92+
$ bin/rails generate open_conference_ware:views layouts rooms
93+
94+
You can see a full list of available arguments by running:
95+
96+
$ bin/rails generate open_conference_ware:views --help
8597

8698
Releases
8799
--------
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
class OpenConferenceWare::ViewsGenerator < Rails::Generators::Base
2+
VIEWS_DIR = OpenConferenceWare::Engine.root.join('app','views')
3+
OCW_VIEW_DIRS = Pathname.glob(VIEWS_DIR.join("open_conference_ware","*/")).map{|d| d.basename.to_s } + ["layouts"]
4+
5+
source_root VIEWS_DIR
6+
desc <<-DESC
7+
Copies OCW's views into your application. By default, all views will be copied.
8+
9+
Optionally, you can pass specific view directories as arguments.
10+
11+
Available directories:
12+
- #{OCW_VIEW_DIRS.join("\n- ")}
13+
14+
DESC
15+
argument :which_views,
16+
type: :array,
17+
default: ["all"],
18+
desc: "Which view directories to copy. One or more of: #{OCW_VIEW_DIRS.join(' ')}",
19+
banner: "VIEWS TO COPY"
20+
21+
def copy_views
22+
directories_to_copy = if which_views.any?{|d| d == "all"}
23+
OCW_VIEW_DIRS
24+
else
25+
which_views.select{|d| OCW_VIEW_DIRS.include?(d) }
26+
end
27+
28+
directories_to_copy.each do |dir|
29+
view_directory dir
30+
end
31+
end
32+
33+
protected
34+
35+
def view_directory(name)
36+
path = if name == "layouts"
37+
"layouts/open_conference_ware"
38+
else
39+
"open_conference_ware/#{name}"
40+
end
41+
42+
directory path, "app/views/#{path}"
43+
end
44+
end

0 commit comments

Comments
 (0)