sethyates-content_manager 0.4.0 → 1.0.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.
- data/README.rdoc +253 -1
- data/VERSION +1 -1
- data/content_manager.gemspec +38 -2
- data/doc/created.rid +1 -0
- data/doc/files/README_rdoc.html +487 -0
- data/doc/fr_class_index.html +26 -0
- data/doc/fr_file_index.html +27 -0
- data/doc/fr_method_index.html +26 -0
- data/doc/index.html +24 -0
- data/doc/rdoc-style.css +208 -0
- data/generators/component_scaffold/USAGE +28 -0
- data/generators/component_scaffold/component_scaffold_generator.rb +84 -0
- data/generators/component_scaffold/templates/controller.rb +85 -0
- data/generators/component_scaffold/templates/model.rb +5 -0
- data/generators/component_scaffold/templates/style.css +1 -0
- data/generators/component_scaffold/templates/view_edit.html.erb +13 -0
- data/generators/component_scaffold/templates/view_index.html.erb +22 -0
- data/generators/component_scaffold/templates/view_new.html.erb +12 -0
- data/generators/component_scaffold/templates/view_show.html.erb +3 -0
- data/generators/content_scaffold/USAGE +28 -0
- data/generators/content_scaffold/content_scaffold_generator.rb +83 -0
- data/generators/content_scaffold/templates/controller.rb +85 -0
- data/generators/content_scaffold/templates/model.rb +8 -0
- data/generators/content_scaffold/templates/view_edit.html.erb +13 -0
- data/generators/content_scaffold/templates/view_index.html.erb +22 -0
- data/generators/content_scaffold/templates/view_new.html.erb +12 -0
- data/generators/content_scaffold/templates/view_show.html.erb +8 -0
- data/lib/component.rb +28 -0
- data/lib/content/adapters/base.rb +79 -0
- data/lib/content/adapters/cabinet_adapter.rb +62 -0
- data/lib/content/adapters/tyrant_adapter.rb +73 -0
- data/lib/content/item.rb +178 -0
- data/lib/content/item_association_class_methods.rb +148 -0
- data/lib/content/item_class_methods.rb +71 -0
- data/lib/content/item_dirty_methods.rb +171 -0
- data/lib/content/item_finder_class_methods.rb +203 -0
- data/lib/content/manager.rb +105 -0
- data/lib/content/sublayout.rb +44 -0
- data/lib/content/template.rb +24 -0
- metadata +38 -2
@@ -0,0 +1 @@
|
|
1
|
+
.<%= singular_name %> {}
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
2
|
+
<%%= f.error_messages %>
|
3
|
+
|
4
|
+
<% for attribute in attributes -%>
|
5
|
+
<%%= f.label :<%= attribute.name %> %>
|
6
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
<%%= f.submit 'Update' %>
|
10
|
+
<%% end %>
|
11
|
+
|
12
|
+
<%%= link_to 'Show', @<%= singular_name %> %> |
|
13
|
+
<%%= link_to 'Back', components_<%= plural_name %>_path %>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<table>
|
2
|
+
<tr>
|
3
|
+
<% for attribute in attributes -%>
|
4
|
+
<th><%= attribute.column.human_name %></th>
|
5
|
+
<% end -%>
|
6
|
+
</tr>
|
7
|
+
|
8
|
+
<%% @<%= plural_name %>.each do |<%= singular_name %>| %>
|
9
|
+
<tr>
|
10
|
+
<% for attribute in attributes -%>
|
11
|
+
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
|
12
|
+
<% end -%>
|
13
|
+
<td><%%= link_to 'Show', <%= singular_name %> %></td>
|
14
|
+
<td><%%= link_to 'Edit', edit_components_<%= singular_name %>_path(<%= singular_name %>) %></td>
|
15
|
+
<td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
16
|
+
</tr>
|
17
|
+
<%% end %>
|
18
|
+
</table>
|
19
|
+
|
20
|
+
<br />
|
21
|
+
|
22
|
+
<%%= link_to 'New <%= singular_name %>', new_components_<%= singular_name %>_path %>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
2
|
+
<%%= f.error_messages %>
|
3
|
+
|
4
|
+
<% for attribute in attributes -%>
|
5
|
+
<%%= f.label :<%= attribute.name %> %>
|
6
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
<%%= f.submit 'Create' %>
|
10
|
+
<%% end %>
|
11
|
+
|
12
|
+
<%%= link_to 'Back', components_<%= plural_name %>_path %>
|
@@ -0,0 +1,28 @@
|
|
1
|
+
Description:
|
2
|
+
Scaffolds an entire component resource, from model to controller and
|
3
|
+
views. The resource is ready to use as a starting point for your
|
4
|
+
RESTful, resource-oriented component.
|
5
|
+
|
6
|
+
Pass the name of the model (in singular form), either CamelCased or
|
7
|
+
under_scored, as the first argument, and an optional list of attribute
|
8
|
+
pairs.
|
9
|
+
|
10
|
+
Attribute pairs are column_name:sql_type arguments specifying the
|
11
|
+
model's attributes.
|
12
|
+
|
13
|
+
You don't have to think up every attribute up front, but it helps to
|
14
|
+
sketch out a few so you can start working with the resource immediately.
|
15
|
+
|
16
|
+
For example, 'component_scaffold header title:string body:text'
|
17
|
+
gives you a model with those three attributes, a controller that handles
|
18
|
+
the create/show/update/destroy, forms to create and edit your headers,
|
19
|
+
and an index that lists them all, as well as a map.resources :headers
|
20
|
+
declaration in config/routes.rb.
|
21
|
+
|
22
|
+
If you want to remove all the generated files, run
|
23
|
+
'script/destroy component_scaffold ModelName'.
|
24
|
+
|
25
|
+
Examples:
|
26
|
+
`./script/generate component_scaffold post`
|
27
|
+
`./script/generate component_scaffold post title:string body:text`
|
28
|
+
`./script/generate component_scaffold purchase amount:decimal`
|
@@ -0,0 +1,83 @@
|
|
1
|
+
class ContentScaffoldGenerator < Rails::Generator::NamedBase
|
2
|
+
default_options :skip_timestamps => true, :skip_migration => true
|
3
|
+
|
4
|
+
attr_reader :controller_name,
|
5
|
+
:controller_class_path,
|
6
|
+
:controller_file_path,
|
7
|
+
:controller_class_nesting,
|
8
|
+
:controller_class_nesting_depth,
|
9
|
+
:controller_class_name,
|
10
|
+
:controller_underscore_name,
|
11
|
+
:controller_singular_name,
|
12
|
+
:controller_plural_name
|
13
|
+
alias_method :controller_file_name, :controller_underscore_name
|
14
|
+
alias_method :controller_table_name, :controller_plural_name
|
15
|
+
|
16
|
+
def initialize(runtime_args, runtime_options = {})
|
17
|
+
super
|
18
|
+
|
19
|
+
if @name == @name.pluralize
|
20
|
+
logger.warning "Plural version of the model detected, using singularized version. Override with --force-plural."
|
21
|
+
@name = @name.singularize
|
22
|
+
end
|
23
|
+
|
24
|
+
@controller_name = "content/#{@name}".pluralize
|
25
|
+
|
26
|
+
base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
|
27
|
+
@controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
|
28
|
+
@controller_singular_name = base_name.singularize
|
29
|
+
if @controller_class_nesting.empty?
|
30
|
+
@controller_class_name = @controller_class_name_without_nesting
|
31
|
+
else
|
32
|
+
@controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def manifest
|
37
|
+
record do |m|
|
38
|
+
# Check for class naming collisions.
|
39
|
+
m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
|
40
|
+
m.class_collisions(class_name)
|
41
|
+
|
42
|
+
# Controller, helper, views, test and stylesheets directories.
|
43
|
+
m.directory(File.join('app/models', controller_class_path))
|
44
|
+
m.directory(File.join('app/controllers', controller_class_path))
|
45
|
+
m.directory(File.join('app/views', controller_class_path, controller_file_name))
|
46
|
+
m.directory(File.join('app/views/errors'))
|
47
|
+
m.directory(File.join('app/views/sublayouts'))
|
48
|
+
m.directory(File.join('public/stylesheets', controller_class_path))
|
49
|
+
|
50
|
+
for action in scaffold_views
|
51
|
+
m.template(
|
52
|
+
"view_#{action}.html.erb",
|
53
|
+
File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
|
54
|
+
)
|
55
|
+
end
|
56
|
+
|
57
|
+
m.template(
|
58
|
+
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
|
59
|
+
)
|
60
|
+
|
61
|
+
m.route_resources table_name
|
62
|
+
|
63
|
+
m.template 'model.rb', File.join('app/models/content', class_path, "#{file_name}.rb")
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
protected
|
68
|
+
# Override with your own usage banner.
|
69
|
+
def banner
|
70
|
+
"Usage: #{$0} component_scaffold ModelName [field:type, field:type]"
|
71
|
+
end
|
72
|
+
|
73
|
+
def add_options!(opt)
|
74
|
+
end
|
75
|
+
|
76
|
+
def scaffold_views
|
77
|
+
%w[ index show new edit ]
|
78
|
+
end
|
79
|
+
|
80
|
+
def model_name
|
81
|
+
class_name.demodulize
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,85 @@
|
|
1
|
+
class <%= controller_class_name %>Controller < ApplicationController
|
2
|
+
# GET /<%= controller_file_path %>
|
3
|
+
# GET /<%= controller_file_path %>.xml
|
4
|
+
def index
|
5
|
+
@<%= table_name %> = <%= controller_class_name %>.all
|
6
|
+
|
7
|
+
respond_to do |format|
|
8
|
+
format.html # index.html.erb
|
9
|
+
format.xml { render :xml => @<%= table_name %> }
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# GET /<%= controller_file_path %>/1
|
14
|
+
# GET /<%= controller_file_path %>/1.xml
|
15
|
+
def show
|
16
|
+
@<%= file_name %> = <%= controller_class_name %>.find(params[:id])
|
17
|
+
|
18
|
+
respond_to do |format|
|
19
|
+
format.html # show.html.erb
|
20
|
+
format.xml { render :xml => @<%= file_name %> }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# GET /<%= controller_file_path %>/new
|
25
|
+
# GET /<%= controller_file_path %>/new.xml
|
26
|
+
def new
|
27
|
+
@<%= file_name %> = <%= controller_class_name %>.new
|
28
|
+
|
29
|
+
respond_to do |format|
|
30
|
+
format.html # new.html.erb
|
31
|
+
format.xml { render :xml => @<%= file_name %> }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
# GET /<%= controller_file_path %>/1/edit
|
36
|
+
def edit
|
37
|
+
@<%= file_name %> = <%= controller_class_name %>.find(params[:id])
|
38
|
+
end
|
39
|
+
|
40
|
+
# POST /<%= controller_file_path %>
|
41
|
+
# POST /<%= controller_file_path %>.xml
|
42
|
+
def create
|
43
|
+
@<%= file_name %> = <%= controller_class_name %>.new(params[:content_<%= file_name %>])
|
44
|
+
|
45
|
+
respond_to do |format|
|
46
|
+
if @<%= file_name %>.save
|
47
|
+
flash[:notice] = '<%= class_name %> was successfully created.'
|
48
|
+
format.html { redirect_to(content_<%= table_name %>_url) }
|
49
|
+
format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
|
50
|
+
else
|
51
|
+
format.html { render :action => "new" }
|
52
|
+
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
# PUT /<%= controller_file_path %>/1
|
58
|
+
# PUT /<%= controller_file_path %>/1.xml
|
59
|
+
def update
|
60
|
+
@<%= file_name %> = <%= controller_class_name %>.find(params[:id])
|
61
|
+
|
62
|
+
respond_to do |format|
|
63
|
+
if @<%= file_name %>.update_attributes(params[:content_<%= file_name %>])
|
64
|
+
flash[:notice] = '<%= class_name %> was successfully updated.'
|
65
|
+
format.html { redirect_to(content_<%= table_name %>_url) }
|
66
|
+
format.xml { head :ok }
|
67
|
+
else
|
68
|
+
format.html { render :action => "edit" }
|
69
|
+
format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
# DELETE /<%= controller_file_path %>/1
|
75
|
+
# DELETE /<%= controller_file_path %>/1.xml
|
76
|
+
def destroy
|
77
|
+
@<%= file_name %> = <%= controller_class_name %>.find(params[:id])
|
78
|
+
@<%= file_name %>.destroy
|
79
|
+
|
80
|
+
respond_to do |format|
|
81
|
+
format.html { redirect_to(content_<%= table_name %>_url) }
|
82
|
+
format.xml { head :ok }
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
2
|
+
<%%= f.error_messages %>
|
3
|
+
|
4
|
+
<% for attribute in attributes -%>
|
5
|
+
<%%= f.label :<%= attribute.name %> %>
|
6
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
<%%= f.submit 'Update' %>
|
10
|
+
<%% end %>
|
11
|
+
|
12
|
+
<%%= link_to 'Show', @<%= singular_name %> %> |
|
13
|
+
<%%= link_to 'Back', content_<%= plural_name %>_path %>
|
@@ -0,0 +1,22 @@
|
|
1
|
+
<table>
|
2
|
+
<tr>
|
3
|
+
<% for attribute in attributes -%>
|
4
|
+
<th><%= attribute.column.human_name %></th>
|
5
|
+
<% end -%>
|
6
|
+
</tr>
|
7
|
+
|
8
|
+
<%% @<%= plural_name %>.each do |<%= singular_name %>| %>
|
9
|
+
<tr>
|
10
|
+
<% for attribute in attributes -%>
|
11
|
+
<td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
|
12
|
+
<% end -%>
|
13
|
+
<td><%%= link_to 'Show', <%= singular_name %> %></td>
|
14
|
+
<td><%%= link_to 'Edit', edit_content_<%= singular_name %>_path(<%= singular_name %>) %></td>
|
15
|
+
<td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
|
16
|
+
</tr>
|
17
|
+
<%% end %>
|
18
|
+
</table>
|
19
|
+
|
20
|
+
<br />
|
21
|
+
|
22
|
+
<%%= link_to 'New <%= singular_name %>', new_content_<%= singular_name %>_path %>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<%% form_for(@<%= singular_name %>) do |f| %>
|
2
|
+
<%%= f.error_messages %>
|
3
|
+
|
4
|
+
<% for attribute in attributes -%>
|
5
|
+
<%%= f.label :<%= attribute.name %> %>
|
6
|
+
<%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
|
7
|
+
<% end -%>
|
8
|
+
|
9
|
+
<%%= f.submit 'Create' %>
|
10
|
+
<%% end %>
|
11
|
+
|
12
|
+
<%%= link_to 'Back', content_<%= plural_name %>_path %>
|
data/lib/component.rb
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
class Component
|
2
|
+
attr_accessor :id, :name, :controller
|
3
|
+
|
4
|
+
def self.all
|
5
|
+
index = 0
|
6
|
+
@components ||= Dir.glob("app/controllers/components/**/*.rb").collect {|path|
|
7
|
+
path = path.gsub("app/controllers/components/", "")
|
8
|
+
}.compact.collect {|path|
|
9
|
+
returning Component.new(path) do |component|
|
10
|
+
component.id = index = index + 1
|
11
|
+
end
|
12
|
+
}
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.find(index)
|
16
|
+
all[index.to_i - 1]
|
17
|
+
end
|
18
|
+
|
19
|
+
def initialize(path)
|
20
|
+
@name = path.gsub('.rb', '')
|
21
|
+
@controller = "components/#{@name}".camelize.constantize
|
22
|
+
@name.gsub!(/_controller$/, '')
|
23
|
+
end
|
24
|
+
|
25
|
+
def to_param
|
26
|
+
id.to_s
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
module Content
|
2
|
+
module Adapters
|
3
|
+
class Base
|
4
|
+
@@row_even = true
|
5
|
+
protected
|
6
|
+
def log_count(query_options, table_name, ms)
|
7
|
+
if @logger && @logger.debug?
|
8
|
+
table_name ||= 'Content::Item'
|
9
|
+
table_name = query_options[:conditions][:content_type] if query_options.has_key? :conditions and query_options[:conditions].has_key? :content_type
|
10
|
+
table_name = table_name.to_s.gsub('::','').tableize
|
11
|
+
sql = "SELECT COUNT(*) FROM #{table_name}"
|
12
|
+
sql = sql + " WHERE #{map_conditions(query_options[:conditions])}" if query_options.has_key? :conditions
|
13
|
+
sql = sql + " ORDER BY #{query_options[:order].inspect}" if query_options.has_key? :order
|
14
|
+
sql = sql + " LIMIT #{query_options[:limit]}" if query_options.has_key? :limit
|
15
|
+
name = '%s (%.1fms)' % [table_name.classify, ms]
|
16
|
+
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def log_select(query_options, table_name, ms)
|
21
|
+
if @logger && @logger.debug?
|
22
|
+
table_name ||= 'Content::Item'
|
23
|
+
table_name = query_options[:conditions][:content_type] if query_options.has_key? :conditions and query_options[:conditions].has_key? :content_type
|
24
|
+
table_name = table_name.to_s.gsub('::','').tableize
|
25
|
+
sql = "SELECT * FROM #{table_name}"
|
26
|
+
sql = sql + " WHERE #{map_conditions(query_options[:conditions])}" if query_options.has_key? :conditions
|
27
|
+
sql = sql + " ORDER BY #{query_options[:order].inspect}" if query_options.has_key? :order
|
28
|
+
sql = sql + " LIMIT #{query_options[:limit]}" if query_options.has_key? :limit
|
29
|
+
name = '%s (%.1fms)' % [table_name.classify, ms]
|
30
|
+
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def log_update(id, attributes, table_name, ms)
|
35
|
+
if @logger && @logger.debug?
|
36
|
+
table_name ||= 'Content::Item'
|
37
|
+
table_name = table_name.to_s.gsub('::','').tableize
|
38
|
+
sql = "UPDATE #{table_name} SET "
|
39
|
+
sql = sql + attributes.reject {|k,v| k.to_s == "__id"}.collect {|k,v| "#{k} = #{v.inspect}" }.join(", ")
|
40
|
+
sql = sql + " WHERE id = #{id}"
|
41
|
+
name = '%s (%.1fms)' % [table_name.classify, ms]
|
42
|
+
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def log_delete(id, table_name, ms)
|
47
|
+
if @logger && @logger.debug?
|
48
|
+
table_name ||= 'Content::Item'
|
49
|
+
table_name = table_name.to_s.gsub('::','').tableize
|
50
|
+
sql = "DELETE #{table_name} WHERE id = #{id}"
|
51
|
+
name = '%s (%.1fms)' % [table_name.classify, ms]
|
52
|
+
@logger.debug(format_log_entry(name, sql.squeeze(' ')))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
56
|
+
def map_conditions(conditions)
|
57
|
+
conditions.reject{|k,v| k == :content_type}.collect{|k,v| "#{k.to_s.gsub('__', '')} = #{v.inspect}"}.join(" AND ")
|
58
|
+
end
|
59
|
+
|
60
|
+
def format_log_entry(message, dump = nil)
|
61
|
+
if ActiveRecord::Base.colorize_logging
|
62
|
+
if @@row_even
|
63
|
+
@@row_even = false
|
64
|
+
message_color, dump_color = "4;36;1", "0;1"
|
65
|
+
else
|
66
|
+
@@row_even = true
|
67
|
+
message_color, dump_color = "4;35;1", "0"
|
68
|
+
end
|
69
|
+
|
70
|
+
log_entry = " \e[#{message_color}m#{message}\e[0m "
|
71
|
+
log_entry << "\e[#{dump_color}m%#{String === dump ? 's' : 'p'}\e[0m" % dump if dump
|
72
|
+
log_entry
|
73
|
+
else
|
74
|
+
"%s %s" % [message, dump]
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|