acts_as_content_node 0.1.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (34) hide show
  1. data/.document +5 -0
  2. data/.gitignore +7 -0
  3. data/LICENSE +20 -0
  4. data/README.rdoc +7 -0
  5. data/Rakefile +57 -0
  6. data/VERSION +1 -0
  7. data/acts_as_content_node.gemspec +74 -0
  8. data/app/helpers/content_nodes_helper.rb +17 -0
  9. data/app/views/content_nodes/_publish_select.html.erb +14 -0
  10. data/generators/content_node_scaffold/USAGE +8 -0
  11. data/generators/content_node_scaffold/content_node_scaffold_generator.rb +64 -0
  12. data/generators/content_node_scaffold/lib/insert_commands.rb +93 -0
  13. data/generators/content_node_scaffold/templates/admin_controller.rb +77 -0
  14. data/generators/content_node_scaffold/templates/admin_index.html.erb +48 -0
  15. data/generators/content_node_scaffold/templates/admin_show.html.erb +31 -0
  16. data/generators/content_node_scaffold/templates/helper.rb +2 -0
  17. data/generators/content_node_scaffold/templates/helper_test.rb +4 -0
  18. data/generators/content_node_scaffold/templates/layout.html.erb +17 -0
  19. data/generators/content_node_scaffold/templates/preview.js.rjs +1 -0
  20. data/generators/content_node_scaffold/templates/style.css +54 -0
  21. data/generators/content_node_scaffold/templates/view_controller.rb +31 -0
  22. data/generators/content_node_scaffold/templates/view_index.html.erb +19 -0
  23. data/generators/content_node_scaffold/templates/view_show.html.erb +7 -0
  24. data/init.rb +1 -0
  25. data/lib/acts_as_content_node/content_node.rb +72 -0
  26. data/lib/acts_as_content_node/publishable.rb +40 -0
  27. data/lib/acts_as_content_node.rb +2 -0
  28. data/rails/init.rb +9 -0
  29. data/tasks/acts_as_content_node_tasks.rake +4 -0
  30. data/test/acts_as_content_node_test.rb +107 -0
  31. data/test/database.yml +21 -0
  32. data/test/schema.rb +18 -0
  33. data/test/test_helper.rb +51 -0
  34. metadata +90 -0
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ test/debug.log
7
+
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Steve England
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,7 @@
1
+ = acts_as_content_node
2
+
3
+ Description goes here.
4
+
5
+ == Copyright
6
+
7
+ Copyright (c) 2009 Steve England. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,57 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "acts_as_content_node"
8
+ gem.summary = %Q{Common functions for a record used as content of a website. Generator for cms and front end}
9
+ gem.description = %Q{Common functions for a record used as content of a website. Generator for cms and front end.}
10
+ gem.email = "steve@wearebeef.co.uk"
11
+ gem.homepage = "http://github.com/beef/acts_as_content_node"
12
+ gem.authors = ["Steve England"]
13
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+
41
+ task :default => :test
42
+
43
+ require 'rake/rdoctask'
44
+ Rake::RDocTask.new do |rdoc|
45
+ if File.exist?('VERSION.yml')
46
+ config = YAML.load(File.read('VERSION.yml'))
47
+ version = "#{config[:major]}.#{config[:minor]}.#{config[:patch]}"
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "acts_as_content_node #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
57
+
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.7
@@ -0,0 +1,74 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{acts_as_content_node}
8
+ s.version = "0.1.7"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Steve England"]
12
+ s.date = %q{2009-10-08}
13
+ s.description = %q{Common functions for a record used as content of a website. Generator for cms and front end.}
14
+ s.email = %q{steve@wearebeef.co.uk}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "acts_as_content_node.gemspec",
27
+ "app/helpers/content_nodes_helper.rb",
28
+ "app/views/content_nodes/_publish_select.html.erb",
29
+ "generators/content_node_scaffold/USAGE",
30
+ "generators/content_node_scaffold/content_node_scaffold_generator.rb",
31
+ "generators/content_node_scaffold/lib/insert_commands.rb",
32
+ "generators/content_node_scaffold/templates/admin_controller.rb",
33
+ "generators/content_node_scaffold/templates/admin_index.html.erb",
34
+ "generators/content_node_scaffold/templates/admin_show.html.erb",
35
+ "generators/content_node_scaffold/templates/helper.rb",
36
+ "generators/content_node_scaffold/templates/helper_test.rb",
37
+ "generators/content_node_scaffold/templates/layout.html.erb",
38
+ "generators/content_node_scaffold/templates/preview.js.rjs",
39
+ "generators/content_node_scaffold/templates/style.css",
40
+ "generators/content_node_scaffold/templates/view_controller.rb",
41
+ "generators/content_node_scaffold/templates/view_index.html.erb",
42
+ "generators/content_node_scaffold/templates/view_show.html.erb",
43
+ "init.rb",
44
+ "lib/acts_as_content_node.rb",
45
+ "lib/acts_as_content_node/content_node.rb",
46
+ "lib/acts_as_content_node/publishable.rb",
47
+ "rails/init.rb",
48
+ "tasks/acts_as_content_node_tasks.rake",
49
+ "test/acts_as_content_node_test.rb",
50
+ "test/database.yml",
51
+ "test/schema.rb",
52
+ "test/test_helper.rb"
53
+ ]
54
+ s.homepage = %q{http://github.com/beef/acts_as_content_node}
55
+ s.rdoc_options = ["--charset=UTF-8"]
56
+ s.require_paths = ["lib"]
57
+ s.rubygems_version = %q{1.3.5}
58
+ s.summary = %q{Common functions for a record used as content of a website. Generator for cms and front end}
59
+ s.test_files = [
60
+ "test/acts_as_content_node_test.rb",
61
+ "test/schema.rb",
62
+ "test/test_helper.rb"
63
+ ]
64
+
65
+ if s.respond_to? :specification_version then
66
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
67
+ s.specification_version = 3
68
+
69
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
70
+ else
71
+ end
72
+ else
73
+ end
74
+ end
@@ -0,0 +1,17 @@
1
+ module ContentNodesHelper
2
+
3
+ def content_status(node)
4
+ if node.published?
5
+ '<span class="status approved">Approved</span>'
6
+ elsif node.published_at.nil?
7
+ '<span class="status draft">Draft</span>'
8
+ else
9
+ '<span class="status hidden">Hidden</span>'
10
+ end
11
+ end
12
+
13
+ def publish_select(form)
14
+ render :partial => '/content_nodes/publish_select', :locals => { :f => form}
15
+ end
16
+
17
+ end
@@ -0,0 +1,14 @@
1
+ <fieldset>
2
+ <legend>Publishing Options</legend>
3
+ <p>
4
+ <label for="article_published_at">Publish at</label><br/>
5
+ <%= check_box_tag 'live', true, params[:live], :onclick => 'toggle_published_at(this.checked)', :id => 'live-from' %>
6
+ <span id="published_at"><%= f.datetime_select 'published_at', :disabled => params[:live] ? nil : 'disabled' %></span>
7
+ </p>
8
+
9
+ <p>
10
+ <label for="article_published_to">Publish till</label><br/>
11
+ <%= check_box_tag 'live', true, params[:live], :onclick => 'toggle_published_to(this.checked)', :id => 'live-to' %>
12
+ <span id="published_to"><%= f.datetime_select 'published_to', :disabled => params[:live] ? nil : 'disabled' %></span>
13
+ </p>
14
+ </fieldset>
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ ./script/generate content_node_scaffold Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,64 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/lib/insert_commands.rb")
2
+
3
+ class ContentNodeScaffoldGenerator < ScaffoldGenerator
4
+
5
+ def initialize(runtime_args, runtime_options = {})
6
+ super
7
+
8
+ @args = ['title:string', 'permalink:string', 'published_at:datetime', 'published_to:datetime'] + @args
9
+ end
10
+
11
+ def manifest
12
+ record do |m|
13
+ # Check for class naming collisions.
14
+ m.class_collisions("#{controller_class_name}Controller", "Admin::#{controller_class_name}Controller", "#{controller_class_name}Helper")
15
+ m.class_collisions(class_name)
16
+
17
+ # Controller, helper, views, test and stylesheets directories.
18
+ m.directory(File.join('app/models', class_path))
19
+ m.directory(File.join('app/controllers', controller_class_path))
20
+ m.directory(File.join('app/controllers/admin', controller_class_path))
21
+ m.directory(File.join('app/helpers', controller_class_path))
22
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
23
+ m.directory(File.join('app/views/admin', controller_class_path, controller_file_name))
24
+ m.directory(File.join('app/views/layouts', controller_class_path))
25
+ m.directory(File.join('test/unit', class_path))
26
+ m.directory(File.join('test/unit/helpers', class_path))
27
+ m.directory(File.join('public/stylesheets', class_path))
28
+
29
+ for action in ['index', 'show']
30
+ m.template(
31
+ "view_#{action}.html.erb",
32
+ File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
33
+ )
34
+ m.template(
35
+ "admin_#{action}.html.erb",
36
+ File.join('app/views/admin', controller_class_path, controller_file_name, "#{action}.html.erb")
37
+ )
38
+ end
39
+ m.template('preview.js.rjs', File.join('app/views/admin', controller_class_path, controller_file_name, 'preview.js.rjs'))
40
+
41
+ # Layout and stylesheet.
42
+ m.template('layout.html.erb', File.join('app/views/layouts', controller_class_path, "application.html.erb"))
43
+ m.template('style.css', 'public/stylesheets/scaffold.css')
44
+
45
+ m.template(
46
+ 'view_controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
47
+ )
48
+ m.template(
49
+ 'admin_controller.rb', File.join('app/controllers/admin', controller_class_path, "#{controller_file_name}_controller.rb")
50
+ )
51
+
52
+ m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
53
+ m.template('helper_test.rb', File.join('test/unit/helpers', controller_class_path, "#{controller_file_name}_helper_test.rb"))
54
+
55
+ m.route_resources ":#{controller_file_name}, :collection => { :preview => :get }"
56
+ m.route_resources_to_namespace('admin', "#{controller_file_name}, :collection => { :preview => :post }, :member => { :preview => :put }")
57
+
58
+ m.dependency 'model', [name, '--skip-fixture'] + @args, :collision => :skip
59
+
60
+ m.insert_into "app/models/#{singular_name}.rb", 'acts_as_content_node'
61
+ end
62
+ end
63
+
64
+ end
@@ -0,0 +1,93 @@
1
+ # Mostly pinched from http://github.com/ryanb/nifty-generators/tree/master
2
+ Rails::Generator::Commands::Base.class_eval do
3
+ def file_contains?(relative_destination, line)
4
+ File.read(destination_path(relative_destination)).include?(line)
5
+ end
6
+ end
7
+
8
+ Rails::Generator::Commands::Create.class_eval do
9
+
10
+ def insert_into(file, line)
11
+ logger.insert "#{line} into #{file}"
12
+ unless options[:pretend] || file_contains?(file, line)
13
+ gsub_file file, /^(class|module) .+$/ do |match|
14
+ "#{match}\n #{line}"
15
+ end
16
+ end
17
+ end
18
+
19
+ def route_resources(resource_list)
20
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
21
+
22
+ logger.route "map.resources #{resource_list}"
23
+ unless options[:pretend] || file_contains?('config/routes.rb', resource_list)
24
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
25
+ "#{match}\n map.resources #{resource_list}"
26
+ end
27
+ end
28
+ end
29
+
30
+ def route_resources_to_namespace(namespace, resource_list)
31
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
32
+
33
+ namespace_map = "map.namespace(:#{namespace}) do |#{namespace}|"
34
+ logger.route namespace_map
35
+ unless options[:pretend] || file_contains?('config/routes.rb', namespace_map)
36
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
37
+ "#{match}\n #{namespace_map}\n end"
38
+ end
39
+ end
40
+
41
+ namespace_route = "#{namespace}.resources :#{resource_list}"
42
+
43
+ logger.route namespace_route
44
+ unless options[:pretend] || file_contains?('config/routes.rb', namespace_route)
45
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(namespace_map)})/mi do |match|
46
+ "#{match}\n #{namespace_route}"
47
+ end
48
+ end
49
+ end
50
+
51
+ end
52
+
53
+ Rails::Generator::Commands::Destroy.class_eval do
54
+
55
+ def route_resources_to_namespace(namespace, resource_list)
56
+ # do ni
57
+ end
58
+
59
+ def route_resources(resource_list)
60
+ look_for = " map.resources #{resource_list}\n".gsub(/[\[\]]/, '\\\\\0')
61
+ logger.route "map.resources #{resource_list} #{look_for}"
62
+ unless options[:pretend]
63
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
64
+ end
65
+ end
66
+
67
+ def insert_into(file, line)
68
+ logger.remove "#{line} from #{file}"
69
+ unless options[:pretend]
70
+ gsub_file file, "\n #{line}", ''
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ Rails::Generator::Commands::List.class_eval do
77
+
78
+ def route_resources_to_namespace(namespace, resource_list)
79
+ namespace_map = "map.namespace(:#{namespace}) do |#{namespace}|"
80
+ logger.route namespace_map
81
+ namespace_route = "#{namespace}.resources :#{resource_list}"
82
+ logger.route namespace_route
83
+ end
84
+
85
+ def route_resources(resources_list)
86
+ logger.route "map.resource #{resource_list}"
87
+ end
88
+
89
+ def insert_into(file, line)
90
+ logger.insert "#{line} into #{file}"
91
+ end
92
+
93
+ end
@@ -0,0 +1,77 @@
1
+ class Admin::<%= controller_class_name %>Controller < Admin::BaseController
2
+ sortable_attributes :<%= attributes.collect{|a| a.name}.join(', :') %>
3
+
4
+ def index
5
+ @<%= table_name %> = <%= class_name %>.paginate :page => params[:page], :order => sort_order
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
+ def show
14
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
15
+
16
+ respond_to do |format|
17
+ format.html
18
+ format.xml { render :xml => @<%= file_name %> }
19
+ end
20
+ end
21
+
22
+ def new
23
+ @<%= file_name %> = <%= class_name %>.new
24
+
25
+ respond_to do |format|
26
+ format.html { render :action =>'show' }
27
+ format.xml { render :xml => @<%= file_name %> }
28
+ end
29
+ end
30
+
31
+ def create
32
+ @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
33
+ @<%= file_name %>.updated_by = @<%= file_name %>.created_by = current_user
34
+
35
+ respond_to do |format|
36
+ if @<%= file_name %>.save
37
+ flash[:notice] = '<%= class_name %> was successfully created.'
38
+ format.html { redirect_to(admin_<%= table_name %>_url) }
39
+ format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
40
+ else
41
+ format.html { render :action => "show" }
42
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
43
+ end
44
+ end
45
+ end
46
+
47
+ def update
48
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
49
+ @<%= file_name %>.updated_by = current_user
50
+
51
+ respond_to do |format|
52
+ if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
53
+ flash[:notice] = '<%= class_name %> was successfully updated.'
54
+ format.html { redirect_to(admin_<%= table_name %>_url) }
55
+ format.xml { head :ok }
56
+ else
57
+ format.html { render :action => "show" }
58
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
59
+ end
60
+ end
61
+ end
62
+
63
+ def destroy
64
+ @<%= file_name %> = <%= class_name %>.find(params[:id])
65
+ @<%= file_name %>.destroy
66
+ flash[:notice] = '<%= class_name %> was successfully deleted.'
67
+
68
+ respond_to do |format|
69
+ format.html { redirect_to(admin_<%= table_name %>_url) }
70
+ format.xml { head :ok }
71
+ end
72
+ end
73
+
74
+ def preview
75
+ session[:<%= file_name %>_preview] = params[:<%= file_name %>]
76
+ end
77
+ end
@@ -0,0 +1,48 @@
1
+ <h1>Listing <%= plural_name %></h1>
2
+
3
+ <ul class="choices">
4
+ <li><%%= link_to 'New <%= singular_name %>', new_admin_<%= singular_name %>_path %></li>
5
+ </ul>
6
+
7
+ <table>
8
+ <thead>
9
+ <tr>
10
+ <%%= sortable_table_header :name => "Title", :sort => "title" %>
11
+ <th>Status</th>
12
+ <th>Author</th>
13
+ <%%= sortable_table_header :name => "Updated", :sort => "updated_at" %>
14
+ <%%= sortable_table_header :name => "Published At", :sort => "published_at" %>
15
+ <%%= sortable_table_header :name => "Published To", :sort => "published_to" %>
16
+ <th colspan="3">Actions</th>
17
+ </tr>
18
+ </thead>
19
+ <tbody>
20
+ <%% @<%= plural_name %>.each do |<%= singular_name %>| %>
21
+ <tr id="<%= singular_name %>-<%%= <%= singular_name %>.id %>">
22
+ <td><%%=h <%= singular_name %>.title %></td>
23
+ <td><%%= content_status(<%= singular_name %>) %></td>
24
+ <td><%%= <%= singular_name %>.author %></td>
25
+ <td><%%= <%= singular_name %>.updated_at.strftime('%d %b') %></td>
26
+ <td><%%= <%= singular_name %>.published_at.strftime('%d %b') unless <%= singular_name %>.published_at.nil? %></td>
27
+ <td><%%= <%= singular_name %>.published_to.strftime('%d %b') unless <%= singular_name %>.published_to.nil? %></td>
28
+ <td><%%= link_to 'Show', <%= singular_name %>_path(<%= singular_name %>.permalink), :class => 'show' %></td>
29
+ <td><%%= link_to 'Edit', admin_<%= singular_name %>_path(<%= singular_name %>), :class => 'edit' %></td>
30
+ <td><%%= link_to 'Destroy', admin_<%= singular_name %>_path(<%= singular_name %>), :confirm => 'Are you sure?', :method => :delete, :class => 'delete' %></td>
31
+ </tr>
32
+ <%% end %>
33
+ </tbody>
34
+ <tfoot>
35
+ <tr>
36
+ <%%= sortable_table_header :name => "Title", :sort => "title" %>
37
+ <th>Status</th>
38
+ <th>Author</th>
39
+ <%%= sortable_table_header :name => "Updated", :sort => "updated_at" %>
40
+ <%%= sortable_table_header :name => "Published At", :sort => "published_at" %>
41
+ <%%= sortable_table_header :name => "Published To", :sort => "published_to" %>
42
+ <th colspan="3">Actions</th>
43
+ </tr>
44
+ </tfoot>
45
+ </table>
46
+
47
+ <%= will_paginate %>
48
+
@@ -0,0 +1,31 @@
1
+ <%% content_for :header do %>
2
+ <%%= javascript_include_tag 'lightwindow' %>
3
+ <%%= stylesheet_link_tag 'lightwindow' %>
4
+ <%% end -%>
5
+
6
+ <h1><%%= @<%= singular_name %>.new_record? ? 'New' : 'Editing' %> <%= singular_name %></h1>
7
+
8
+ <%% form_for([:admin, @<%= singular_name %>]) do |f| %>
9
+ <%%= f.error_messages %>
10
+
11
+ <p>
12
+ <%%= f.label :title %><br/>
13
+ <%%= f.text_field :title, :class => 'title' %>
14
+ </p>
15
+
16
+ <% for attribute in attributes.reject{ |a| ['title', 'published_at', 'published_to', 'permalink' ].include?(a.name) } -%>
17
+ <p>
18
+ <%%= f.label :<%= attribute.name %> %><br />
19
+ <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
20
+ </p>
21
+ <% end -%>
22
+
23
+ <%%= publish_select(f) %>
24
+
25
+ <p class="submission">
26
+ <%%= preview_link(@<%= singular_name %>) %>
27
+ <%%= f.submit 'Publish', :name => '<%= singular_name %>[publish]' %>
28
+ <%%= f.submit 'Save as draft', :name => '<%= singular_name %>[hide]' %>
29
+ or <%%= link_to 'Back', admin_<%= plural_name %>_path %>
30
+ </p>
31
+ <%% end %>
@@ -0,0 +1,2 @@
1
+ module <%= controller_class_name %>Helper
2
+ end
@@ -0,0 +1,4 @@
1
+ require 'test_helper'
2
+
3
+ class <%= controller_class_name %>HelperTest < ActionView::TestCase
4
+ end
@@ -0,0 +1,17 @@
1
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
+
4
+ <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
5
+ <head>
6
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
7
+ <title><%= controller_class_name %>: <%%= controller.action_name %></title>
8
+ <%%= stylesheet_link_tag 'scaffold' %>
9
+ </head>
10
+ <body>
11
+
12
+ <p style="color: green"><%%= flash[:notice] %></p>
13
+
14
+ <%%= yield %>
15
+
16
+ </body>
17
+ </html>
@@ -0,0 +1 @@
1
+ page << "myLightWindow.activateWindow({href: '#{preview_<%= table_name %>_path}', title: 'This is only a preview'});"
@@ -0,0 +1,54 @@
1
+ body { background-color: #fff; color: #333; }
2
+
3
+ body, p, ol, ul, td {
4
+ font-family: verdana, arial, helvetica, sans-serif;
5
+ font-size: 13px;
6
+ line-height: 18px;
7
+ }
8
+
9
+ pre {
10
+ background-color: #eee;
11
+ padding: 10px;
12
+ font-size: 11px;
13
+ }
14
+
15
+ a { color: #000; }
16
+ a:visited { color: #666; }
17
+ a:hover { color: #fff; background-color:#000; }
18
+
19
+ .fieldWithErrors {
20
+ padding: 2px;
21
+ background-color: red;
22
+ display: table;
23
+ }
24
+
25
+ #errorExplanation {
26
+ width: 400px;
27
+ border: 2px solid red;
28
+ padding: 7px;
29
+ padding-bottom: 12px;
30
+ margin-bottom: 20px;
31
+ background-color: #f0f0f0;
32
+ }
33
+
34
+ #errorExplanation h2 {
35
+ text-align: left;
36
+ font-weight: bold;
37
+ padding: 5px 5px 5px 15px;
38
+ font-size: 12px;
39
+ margin: -7px;
40
+ background-color: #c00;
41
+ color: #fff;
42
+ }
43
+
44
+ #errorExplanation p {
45
+ color: #333;
46
+ margin-bottom: 0;
47
+ padding: 5px;
48
+ }
49
+
50
+ #errorExplanation ul li {
51
+ font-size: 12px;
52
+ list-style: square;
53
+ }
54
+
@@ -0,0 +1,31 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+ # GET /<%= table_name %>
3
+ # GET /<%= table_name %>.xml
4
+ def index
5
+ @<%= table_name %> = <%= class_name %>.paginate :page => params[:page]
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 /<%= table_name %>/1
14
+ # GET /<%= table_name %>/1.xml
15
+ def show
16
+ @<%= file_name %> = <%= class_name %>.find_by_permalink(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
+ def preview
25
+ @page_id = '<%= table_name %>'
26
+ @page_class = 'show'
27
+ @<%= file_name %> = <%= class_name %>.new(session[:<%= file_name %>_preview])
28
+ session[:<%= file_name %>_preview] = nil
29
+ render :action => "show"
30
+ end
31
+ end
@@ -0,0 +1,19 @@
1
+ <h1>Listing <%= plural_name %></h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <% for attribute in attributes -%>
6
+ <th><%= attribute.column.human_name %></th>
7
+ <% end -%>
8
+ </tr>
9
+
10
+ <%% @<%= plural_name %>.each do |<%= singular_name %>| %>
11
+ <tr>
12
+ <% for attribute in attributes -%>
13
+ <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
14
+ <% end -%>
15
+ </tr>
16
+ <%% end %>
17
+ </table>
18
+
19
+ <%= will_paginate %>
@@ -0,0 +1,7 @@
1
+ <% for attribute in attributes -%>
2
+ <p>
3
+ <b><%= attribute.column.human_name %>:</b>
4
+ <%%=h @<%= singular_name %>.<%= attribute.name %> %>
5
+ </p>
6
+
7
+ <% end -%>
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,72 @@
1
+ module Beef
2
+ module Acts
3
+ module ContentNode
4
+ module ClassMethods
5
+ def acts_as_content_node
6
+
7
+ belongs_to :updated_by, :class_name => 'User'
8
+ belongs_to :created_by, :class_name => 'User'
9
+
10
+ named_scope :authored_by, lambda { |user|
11
+ return {} if user.nil?
12
+ user = User.find(user) unless user.is_a? ::User
13
+ { :conditions => { :created_by_id => user.id } }
14
+ }
15
+
16
+ before_validation :set_url
17
+
18
+ validates_presence_of :title
19
+ validates_uniqueness_of :permalink, :message => 'has been used before', :if => (:permalink_written && :publish)
20
+
21
+ attr_reader :permalink_written
22
+
23
+ def find_by_permalink(permalink)
24
+ content_node = find(:first, :conditions => ['permalink = ?', permalink])
25
+ raise ActiveRecord::RecordNotFound, "Couldn't find #{name} with permalink #{permalink}" if content_node.nil?
26
+ content_node
27
+ end
28
+ acts_as_publishable
29
+
30
+ send :include, InstanceMethods
31
+ end
32
+ end
33
+
34
+ module InstanceMethods
35
+
36
+ def permalink=(permalink)
37
+ unless permalink.blank?
38
+ write_attribute :permalink, permalink.parameterize
39
+ @permalink_written = true
40
+ end
41
+ end
42
+
43
+ def editor
44
+ updated_by.nil? ? 'Anon' : updated_by.read_attribute(:name)
45
+ end
46
+
47
+ def author
48
+ created_by.nil? ? 'Anon' : created_by.read_attribute(:name)
49
+ end
50
+
51
+ def short_desc
52
+ unless description.nil?
53
+ description.split('.').first
54
+ else
55
+ ''
56
+ end
57
+ end
58
+
59
+ private
60
+
61
+ def set_url
62
+ write_attribute :permalink, title.parameterize unless permalink_written or !title_changed?
63
+ end
64
+
65
+ end
66
+
67
+ def self.included(base)
68
+ base.extend ClassMethods
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,40 @@
1
+ module Beef
2
+ module Acts
3
+ module Publishable
4
+ module ClassMethods
5
+ def acts_as_publishable
6
+ send :include, InstanceMethods
7
+
8
+ named_scope :published, lambda { { :conditions => ["(published_at IS NOT NULL AND published_at != '') AND published_at < ? AND (published_to > ? OR published_to IS NULL OR published_to = '')", Time.now, Time.now] } }
9
+ named_scope :draft, :conditions => { :published_at => nil }
10
+
11
+ before_save :set_published
12
+
13
+ attr_accessor :publish, :hide
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+
19
+ def published?
20
+ return false if published_at.nil?
21
+ @published ||= published_at < Time.zone.now
22
+ end
23
+
24
+ private
25
+
26
+ def set_published
27
+ write_attribute :published_at, Time.zone.now if @publish and published_at.nil?
28
+ if @hide
29
+ write_attribute :published_at, nil
30
+ write_attribute :published_to, nil
31
+ end
32
+ end
33
+ end
34
+
35
+ def self.included(base)
36
+ base.extend ClassMethods
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,2 @@
1
+ require "acts_as_content_node/content_node"
2
+ require "acts_as_content_node/publishable"
data/rails/init.rb ADDED
@@ -0,0 +1,9 @@
1
+ require "acts_as_content_node/content_node"
2
+ require "acts_as_content_node/publishable"
3
+
4
+ config.to_prepare do
5
+ ApplicationController.helper(ContentNodesHelper)
6
+ end
7
+
8
+ ActiveRecord::Base.send :include, Beef::Acts::ContentNode
9
+ ActiveRecord::Base.send :include, Beef::Acts::Publishable
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ task :acts_as_content_node do
3
+ # Task goes here
4
+ end
@@ -0,0 +1,107 @@
1
+ require 'test_helper'
2
+
3
+ class ContentNodeTest < Test::Unit::TestCase
4
+ should_validate_presence_of :title
5
+
6
+ should_belong_to :updated_by
7
+ should_belong_to :created_by
8
+
9
+ context "With an exisiting content node" do
10
+ setup do
11
+ @content_node = Factory(:content_node)
12
+ end
13
+
14
+ subject { @content_node }
15
+
16
+ should_validate_uniqueness_of :title, :message => 'has been used before'
17
+
18
+ should "be found by user" do
19
+ assert_equal [@content_node], ContentNode.authored_by(@content_node.created_by)
20
+ end
21
+
22
+ should "be found by user id" do
23
+ assert_equal [@content_node], ContentNode.authored_by(@content_node.created_by.id)
24
+ end
25
+
26
+ should "be found by all if user nil" do
27
+ assert !ContentNode.authored_by(nil).empty?
28
+ end
29
+
30
+ end
31
+
32
+
33
+ context 'When permalink is not set it' do
34
+ setup do
35
+ @title = 'My nice article'
36
+ @content_node = Factory(:content_node, :title => @title)
37
+ end
38
+
39
+ should "be based on title" do
40
+ assert_equal @content_node.permalink, @title.parameterize
41
+ end
42
+
43
+ should "be based on permalink set when updated" do
44
+ @content_node.update_attributes(:permalink => 'a-perma')
45
+ assert_equal @content_node.permalink, 'a-perma'.parameterize
46
+ end
47
+
48
+ teardown do
49
+ @content_node.destroy
50
+ end
51
+ end
52
+
53
+ context 'When permalink is set' do
54
+ setup do
55
+ @permalink = 'something-else'
56
+ @content_node = Factory(:content_node, :permalink => @permalink)
57
+ end
58
+ subject { @content_node }
59
+
60
+ should_validate_uniqueness_of :permalink, :message => 'has been used before'
61
+
62
+ should "be the permalink" do
63
+ assert_equal @content_node.permalink, @permalink.parameterize
64
+ end
65
+
66
+ should "be based on title if updated and title changed" do
67
+ @content_node = ContentNode.first
68
+ @content_node.update_attributes(:title => 'New Title')
69
+ assert_equal @content_node.permalink, 'New Title'.parameterize
70
+ end
71
+
72
+ should "be the permalink if updated and title not changed" do
73
+ @content_node = ContentNode.first
74
+ @content_node.save
75
+ assert_equal @content_node.permalink, @permalink.parameterize
76
+ end
77
+
78
+ teardown do
79
+ @content_node.destroy
80
+ end
81
+ end
82
+
83
+ context 'When publish is set' do
84
+ setup do
85
+ @content_node = Factory(:content_node, :publish => true)
86
+ end
87
+
88
+ should "be published" do
89
+ assert @content_node.published?
90
+ end
91
+
92
+ should "be found in published scope" do
93
+ assert !ContentNode.published.find(@content_node.id).nil?
94
+ end
95
+
96
+ should "not be published if updated and hide set" do
97
+ @content_node.update_attributes(:hide => true)
98
+ assert !@content_node.published?
99
+ end
100
+
101
+
102
+ # teardown do
103
+ # @content_node.destroy
104
+ # end
105
+ end
106
+
107
+ end
data/test/database.yml ADDED
@@ -0,0 +1,21 @@
1
+ sqlite:
2
+ :adapter: sqlite
3
+ :dbfile: acts_as_content_node_plugin.sqlite.db
4
+ sqlite3:
5
+ :adapter: sqlite3
6
+ :dbfile: acts_as_content_node_plugin.sqlite3.db
7
+ sqlite3mem:
8
+ :adapter: sqlite3
9
+ :dbfile: ":memory:"
10
+ postgresql:
11
+ :adapter: postgresql
12
+ :username: postgres
13
+ :password: postgres
14
+ :database: acts_as_content_node_plugin_test
15
+ :min_messages: ERROR
16
+ mysql:
17
+ :adapter: mysql
18
+ :host: localhost
19
+ :username: root
20
+ :password:
21
+ :database: acts_as_content_node_plugin_test
data/test/schema.rb ADDED
@@ -0,0 +1,18 @@
1
+ ActiveRecord::Schema.define(:version => 0) do
2
+ create_table :content_nodes, :force => true do |t|
3
+ t.string :title
4
+ t.string :permalink
5
+ t.string :description
6
+ t.text :body
7
+ t.datetime :published_at
8
+ t.datetime :published_to
9
+ t.datetime :created_at
10
+ t.datetime :updated_at
11
+ t.references :updated_by, :created_by
12
+ end
13
+
14
+ create_table :users, :force => true do |t|
15
+ t.string :name
16
+ t.string :permalink
17
+ end
18
+ end
@@ -0,0 +1,51 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_record'
4
+ require 'shoulda/rails'
5
+ require 'factory_girl'
6
+ require 'faker'
7
+
8
+ # Setting this makes parameterize work
9
+ $KCODE = 'UTF8'
10
+
11
+ # Makes TimeZone work
12
+ Time.zone = 'UTC'
13
+
14
+ RAILS_DEFAULT_LOGGER = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
15
+ ActiveRecord::Base.logger = RAILS_DEFAULT_LOGGER
16
+
17
+ require "acts_as_content_node/content_node"
18
+ require "acts_as_content_node/publishable"
19
+ require "acts_as_content_node/permalinks"
20
+
21
+ ActiveRecord::Base.send :include, Beef::Acts::ContentNode
22
+ ActiveRecord::Base.send :include, Beef::Acts::Publishable
23
+
24
+ ActiveRecord::Base.configurations = YAML::load(IO.read(File.dirname(__FILE__) + "/database.yml"))
25
+ ActiveRecord::Base.establish_connection(ENV["DB"] || "sqlite3mem")
26
+ ActiveRecord::Migration.verbose = false
27
+ load("schema.rb")
28
+
29
+ class ContentNode < ActiveRecord::Base
30
+ acts_as_content_node
31
+ end
32
+
33
+ class User < ActiveRecord::Base
34
+ before_save :set_permalink
35
+
36
+ def set_permalink
37
+ self.permalink = name.parameterize
38
+ end
39
+ end
40
+
41
+ Factory.define(:content_node) do |content_node|
42
+ content_node.title {Faker::Lorem.words(5).join(' ')}
43
+ content_node.description {Faker::Lorem.sentence}
44
+ content_node.body {Faker::Lorem.paragraphs.join}
45
+ content_node.association :created_by, :factory => :user
46
+ end
47
+
48
+ Factory.define :user do |user|
49
+ user.name { Faker::Name.name }
50
+ end
51
+
metadata ADDED
@@ -0,0 +1,90 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_content_node
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.7
5
+ platform: ruby
6
+ authors:
7
+ - Steve England
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-08 00:00:00 +01:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Common functions for a record used as content of a website. Generator for cms and front end.
17
+ email: steve@wearebeef.co.uk
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - LICENSE
24
+ - README.rdoc
25
+ files:
26
+ - .document
27
+ - .gitignore
28
+ - LICENSE
29
+ - README.rdoc
30
+ - Rakefile
31
+ - VERSION
32
+ - acts_as_content_node.gemspec
33
+ - app/helpers/content_nodes_helper.rb
34
+ - app/views/content_nodes/_publish_select.html.erb
35
+ - generators/content_node_scaffold/USAGE
36
+ - generators/content_node_scaffold/content_node_scaffold_generator.rb
37
+ - generators/content_node_scaffold/lib/insert_commands.rb
38
+ - generators/content_node_scaffold/templates/admin_controller.rb
39
+ - generators/content_node_scaffold/templates/admin_index.html.erb
40
+ - generators/content_node_scaffold/templates/admin_show.html.erb
41
+ - generators/content_node_scaffold/templates/helper.rb
42
+ - generators/content_node_scaffold/templates/helper_test.rb
43
+ - generators/content_node_scaffold/templates/layout.html.erb
44
+ - generators/content_node_scaffold/templates/preview.js.rjs
45
+ - generators/content_node_scaffold/templates/style.css
46
+ - generators/content_node_scaffold/templates/view_controller.rb
47
+ - generators/content_node_scaffold/templates/view_index.html.erb
48
+ - generators/content_node_scaffold/templates/view_show.html.erb
49
+ - init.rb
50
+ - lib/acts_as_content_node.rb
51
+ - lib/acts_as_content_node/content_node.rb
52
+ - lib/acts_as_content_node/publishable.rb
53
+ - rails/init.rb
54
+ - tasks/acts_as_content_node_tasks.rake
55
+ - test/acts_as_content_node_test.rb
56
+ - test/database.yml
57
+ - test/schema.rb
58
+ - test/test_helper.rb
59
+ has_rdoc: true
60
+ homepage: http://github.com/beef/acts_as_content_node
61
+ licenses: []
62
+
63
+ post_install_message:
64
+ rdoc_options:
65
+ - --charset=UTF-8
66
+ require_paths:
67
+ - lib
68
+ required_ruby_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: "0"
73
+ version:
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: "0"
79
+ version:
80
+ requirements: []
81
+
82
+ rubyforge_project:
83
+ rubygems_version: 1.3.5
84
+ signing_key:
85
+ specification_version: 3
86
+ summary: Common functions for a record used as content of a website. Generator for cms and front end
87
+ test_files:
88
+ - test/acts_as_content_node_test.rb
89
+ - test/schema.rb
90
+ - test/test_helper.rb