itium-theme 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
@@ -3,84 +3,68 @@ require 'rails/generators/generated_attribute'
|
|
3
3
|
module ItiumTheme
|
4
4
|
class ThemedGenerator < Rails::Generators::Base
|
5
5
|
source_root File.expand_path('../templates', __FILE__)
|
6
|
-
|
6
|
+
|
7
7
|
argument :controller_path, :type => :string
|
8
|
-
argument :model_name, :type => :string, :required => false
|
9
|
-
|
8
|
+
argument :model_name, :type => :string, :required => false
|
9
|
+
|
10
10
|
class_option :will_paginate, :type => :boolean, :default => false, :desc => 'Specify if you use will_paginate'
|
11
11
|
class_option :themed_type, :type => :string, :default => 'crud', :desc => 'Specify the themed type, crud or text. Default is crud'
|
12
|
-
|
13
12
|
def initialize(args, *options)
|
14
13
|
super(args, *options)
|
15
14
|
initialize_views_variables
|
16
15
|
end
|
17
|
-
|
16
|
+
|
18
17
|
def copy_views
|
19
|
-
generate_views
|
20
|
-
|
21
|
-
if options.engine =~ /erb/
|
22
|
-
gsub_file(File.join('app/views/layouts', "#{options[:layout]}.html.#{options.engine}"), /\<div\s+id=\"main-navigation\">.*\<\/ul\>/mi) do |match|
|
23
|
-
match.gsub!(/\<\/ul\>/, "")
|
24
|
-
%|#{match} <li class="<%= controller.controller_path == '#{@controller_file_path}' ? 'active' : '' %>"><a href="<%= #{controller_routing_path}_path %>">#{plural_model_name}</a></li></ul>|
|
25
|
-
end
|
26
|
-
elsif options.engine =~ /haml/
|
27
|
-
gsub_file(File.join('app/views/layouts', "#{options[:layout]}.html.#{options.engine}"), /#main-navigation.*#wrapper.wat-cf/mi) do |match|
|
28
|
-
match.gsub!(/ #wrapper.wat-cf/, "")
|
29
|
-
%|#{match}| +
|
30
|
-
" "*6 + %|%li{:class => controller.controller_path == '#{@controller_file_path}' ? 'active' : '' }\n| +
|
31
|
-
" "*7 + %|%a{:href => #{controller_routing_path}_path} #{plural_model_name}\n| +
|
32
|
-
" "*3 + %|#wrapper.wat-cf|
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
18
|
+
generate_views
|
19
|
+
|
36
20
|
end
|
37
21
|
|
38
|
-
|
39
|
-
|
22
|
+
protected
|
23
|
+
|
40
24
|
def initialize_views_variables
|
41
25
|
@base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(controller_path)
|
42
26
|
@controller_routing_path = @controller_file_path.gsub(/\//, '_')
|
43
27
|
@model_name = @base_name.singularize unless @model_name
|
44
28
|
@model_name = @model_name.camelize
|
45
29
|
end
|
46
|
-
|
30
|
+
|
47
31
|
def controller_routing_path
|
48
32
|
@controller_routing_path
|
49
33
|
end
|
50
|
-
|
34
|
+
|
51
35
|
def singular_controller_routing_path
|
52
36
|
@controller_routing_path.singularize
|
53
37
|
end
|
54
|
-
|
38
|
+
|
55
39
|
def model_name
|
56
40
|
@model_name
|
57
41
|
end
|
58
|
-
|
42
|
+
|
59
43
|
def plural_model_name
|
60
44
|
@model_name.pluralize
|
61
45
|
end
|
62
|
-
|
46
|
+
|
63
47
|
def resource_name
|
64
48
|
@model_name.underscore
|
65
49
|
end
|
66
|
-
|
50
|
+
|
67
51
|
def plural_resource_name
|
68
52
|
resource_name.pluralize
|
69
53
|
end
|
70
|
-
|
71
|
-
##
|
54
|
+
|
55
|
+
##
|
72
56
|
# Attempts to call #columns on the model's class
|
73
57
|
# If the (Active Record) #columns method does not exist, it attempts to
|
74
58
|
# perform the (Mongoid) #fields method instead
|
75
59
|
def columns
|
76
60
|
begin
|
77
61
|
excluded_column_names = %w[id created_at updated_at]
|
78
|
-
Kernel.const_get(@model_name).columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| Rails::Generators::GeneratedAttribute.new(c.name, c.type)}
|
62
|
+
Kernel.const_get(@model_name).columns.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| Rails::Generators::GeneratedAttribute.new(c.name, c.type)}
|
79
63
|
rescue NoMethodError
|
80
64
|
Kernel.const_get(@model_name).fields.collect{|c| c[1]}.reject{|c| excluded_column_names.include?(c.name) }.collect{|c| Rails::Generators::GeneratedAttribute.new(c.name, c.type.to_s)}
|
81
65
|
end
|
82
66
|
end
|
83
|
-
|
67
|
+
|
84
68
|
def extract_modules(name)
|
85
69
|
modules = name.include?('/') ? name.split('/') : name.split('::')
|
86
70
|
name = modules.pop
|
@@ -89,7 +73,7 @@ module ItiumTheme
|
|
89
73
|
nesting = modules.map { |m| m.camelize }.join('::')
|
90
74
|
[name, path, file_path, nesting, modules.size]
|
91
75
|
end
|
92
|
-
|
76
|
+
|
93
77
|
def generate_views
|
94
78
|
views = {
|
95
79
|
'crud' => {
|
@@ -107,12 +91,11 @@ module ItiumTheme
|
|
107
91
|
selected_views = views[options.themed_type]
|
108
92
|
generate_erb_views(selected_views)
|
109
93
|
end
|
110
|
-
|
94
|
+
|
111
95
|
def generate_erb_views(views)
|
112
96
|
views.each do |template_name, output_path|
|
113
97
|
template template_name, output_path
|
114
98
|
end
|
115
99
|
end
|
116
|
-
|
117
|
-
|
100
|
+
end
|
118
101
|
end
|
data/lib/itium-theme/version.rb
CHANGED