crudify 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (64) hide show
  1. data/.gitignore +4 -2
  2. data/Gemfile.lock +57 -28
  3. data/README.md +121 -3
  4. data/Rakefile +3 -4
  5. data/crudify.gemspec +5 -2
  6. data/lib/crudify.rb +5 -1
  7. data/lib/crudify/base.rb +8 -7
  8. data/lib/crudify/class_methods.rb +35 -21
  9. data/lib/crudify/hook_methods.rb +30 -33
  10. data/lib/crudify/version.rb +1 -1
  11. data/test/dummy/Rakefile +7 -0
  12. data/test/dummy/app/controllers/admin/peanut_butters_controller.rb +5 -0
  13. data/test/dummy/app/controllers/application_controller.rb +3 -0
  14. data/test/dummy/app/controllers/jellies_controller.rb +7 -0
  15. data/test/dummy/app/models/jelly.rb +9 -0
  16. data/test/dummy/app/models/peanut_butter.rb +8 -0
  17. data/test/dummy/app/views/admin/peanut_butters/_fields.html.erb +4 -0
  18. data/test/dummy/app/views/admin/peanut_butters/edit.html.erb +11 -0
  19. data/test/dummy/app/views/admin/peanut_butters/index.html.erb +23 -0
  20. data/test/dummy/app/views/admin/peanut_butters/new.html.erb +11 -0
  21. data/test/dummy/app/views/admin/peanut_butters/show.html.erb +14 -0
  22. data/test/dummy/app/views/jellies/_fields.html.erb +8 -0
  23. data/test/dummy/app/views/jellies/edit.html.erb +11 -0
  24. data/test/dummy/app/views/jellies/index.html.erb +23 -0
  25. data/test/dummy/app/views/jellies/new.html.erb +11 -0
  26. data/test/dummy/app/views/jellies/show.html.erb +15 -0
  27. data/test/dummy/app/views/layouts/application.html.erb +72 -0
  28. data/test/dummy/app/views/shared/_error_messages.html.erb +9 -0
  29. data/test/dummy/config.ru +4 -0
  30. data/test/dummy/config/application.rb +45 -0
  31. data/test/dummy/config/boot.rb +10 -0
  32. data/test/dummy/config/database.yml +22 -0
  33. data/test/dummy/config/environment.rb +5 -0
  34. data/test/dummy/config/environments/development.rb +26 -0
  35. data/test/dummy/config/environments/production.rb +49 -0
  36. data/test/dummy/config/environments/test.rb +38 -0
  37. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  38. data/test/dummy/config/initializers/green_eggs_and_spam.rb +2 -0
  39. data/test/dummy/config/initializers/inflections.rb +10 -0
  40. data/test/dummy/config/initializers/mime_types.rb +5 -0
  41. data/test/dummy/config/initializers/secret_token.rb +7 -0
  42. data/test/dummy/config/initializers/session_store.rb +8 -0
  43. data/test/dummy/config/routes.rb +11 -0
  44. data/test/dummy/db/migrate/20110223073415_create_jellies.rb +13 -0
  45. data/test/dummy/db/migrate/20110223073427_create_peanut_butters.rb +12 -0
  46. data/test/dummy/db/schema.rb +28 -0
  47. data/test/dummy/public/404.html +26 -0
  48. data/test/dummy/public/422.html +26 -0
  49. data/test/dummy/public/500.html +26 -0
  50. data/{log/development.log → test/dummy/public/favicon.ico} +0 -0
  51. data/test/dummy/public/javascripts/application.js +2 -0
  52. data/test/dummy/public/javascripts/controls.js +965 -0
  53. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  54. data/test/dummy/public/javascripts/effects.js +1123 -0
  55. data/test/dummy/public/javascripts/index.html +239 -0
  56. data/test/dummy/public/javascripts/prototype.js +6001 -0
  57. data/test/dummy/public/javascripts/rails.js +191 -0
  58. data/test/dummy/public/stylesheets/styles.css +126 -0
  59. data/test/dummy/script/rails +6 -0
  60. data/test/functional/jellies_controller_test.rb +55 -0
  61. data/test/helper.rb +42 -31
  62. data/test/integration/demo_test.rb +79 -0
  63. data/test/{test_controller.rb → unit/crudify_test.rb} +17 -17
  64. metadata +149 -13
@@ -6,68 +6,73 @@ module Crudify
6
6
 
7
7
  def before_create
8
8
  # just a hook!
9
- puts "> Crud::before_create"
9
+ puts "> Crud::before_create" if @crud_options[:log]
10
10
  before_action
11
11
  end
12
12
 
13
13
  def before_update
14
14
  # just a hook!
15
- puts "> Crud::before_update"
15
+ puts "> Crud::before_update" if @crud_options[:log]
16
16
  before_action
17
17
  end
18
18
 
19
19
  def before_destroy
20
20
  # just a hook!
21
- puts "> Crud::before_destroy"
21
+ puts "> Crud::before_destroy" if @crud_options[:log]
22
+ set_what
22
23
  before_action
23
24
  end
24
-
25
-
25
+
26
26
  def before_action
27
27
  # just a hook!
28
- puts "> Crud::before_action"
29
- @what = @crud_options[:use_class_name_as_title] ? @instance.class.to_s.humanize : @instance.send(@crud_options[:title_attribute].to_sym).inspect
30
- true
28
+ puts "> Crud::before_action" if @crud_options[:log]
29
+ #true
31
30
  end
32
31
 
33
32
 
34
33
 
35
34
  def successful_create
36
- puts "> Crud::successful_create"
37
-
38
- flash[:notice] = t('crudify.created', :what => @what)
35
+ puts "> Crud::successful_create" if @crud_options[:log]
36
+
37
+ flash[:notice] = t('crudify.created', :what => set_what)
39
38
 
40
39
  after_success
41
40
  end
42
41
 
43
42
  def successful_update
44
- puts "> Crud::successful_update"
43
+ puts "> Crud::successful_update" if @crud_options[:log]
45
44
 
46
- flash[:notice] = t('crudify.updated', :what => @what)
45
+ flash[:notice] = t('crudify.updated', :what => set_what)
47
46
 
48
47
  after_success
49
48
  end
50
49
 
51
50
  def successful_destroy
52
- puts "> Crud::successful_destroy"
53
-
51
+ puts "> Crud::successful_destroy" if @crud_options[:log]
54
52
  flash.notice = t('crudify.destroyed', :what => @what)
55
53
 
56
54
  after_success
57
55
  end
58
56
 
59
57
  def after_success
60
- puts "> Crud::after_success"
58
+ puts "> Crud::after_success" if @crud_options[:log]
61
59
 
62
60
  unless request.xhr?
63
- if params[:commit].match(/continue/)
61
+ if @redirect_to_url
62
+ redirect_to @redirect_to_url
63
+ elsif params[:commit].to_s.match(/continue/)
64
64
  if params[:action] == 'create'
65
65
  redirect_to request.referer.sub('new', "#{@what.to_param}/edit")
66
66
  else
67
67
  redirect_to request.referer
68
68
  end
69
69
  else
70
- redirect_back_or_default eval(@crud_options[:redirect_to_url])
70
+ url = eval(@crud_options[:redirect_to_url])
71
+ if defined?(redirect_back_or_default)
72
+ redirect_back_or_default url
73
+ else
74
+ redirect_to url
75
+ end
71
76
  end
72
77
  else
73
78
  render :partial => "/shared/message"
@@ -79,33 +84,25 @@ module Crudify
79
84
 
80
85
 
81
86
  def failed_create
82
- puts "> Crud::failed_create"
83
-
84
- flash[:error] = t('crudify.failed_create', :what => @what)
85
-
87
+ puts "> Crud::failed_create" if @crud_options[:log]
88
+ flash[:error] = t('crudify.failed_create', :what => set_what)
86
89
  after_fail
87
90
  end
88
91
 
89
92
  def failed_update
90
- puts "> Crud::failed_update"
91
-
92
- flash[:error] = t('crudify.failed_update', :what => @what)
93
-
93
+ puts "> Crud::failed_update" if @crud_options[:log]
94
+ flash[:error] = t('crudify.failed_update', :what => set_what)
94
95
  after_fail
95
96
  end
96
97
 
97
98
  def failed_destroy
98
- puts "> Crud::failed_destroy"
99
-
99
+ puts "> Crud::failed_destroy" if @crud_options[:log]
100
100
  flash[:error] = t('crudify.failed_destroy', :what => @what)
101
-
102
101
  after_fail
103
102
  end
104
-
105
-
103
+
106
104
  def after_fail
107
- puts "> Crud::after_fail"
108
-
105
+ puts "> Crud::after_fail" if @crud_options[:log]
109
106
  unless request.xhr?
110
107
  render :action => request.post? ? 'new' : 'edit'
111
108
  else
@@ -1,3 +1,3 @@
1
1
  module Crudify
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -0,0 +1,7 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,5 @@
1
+ class Admin::PeanutButtersController < ApplicationController
2
+
3
+ crudify :peanut_butter, :use_class_name_as_title => true
4
+
5
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,7 @@
1
+ class JelliesController < ApplicationController
2
+
3
+ crudify :jelly,
4
+ :redirect_to_url => "jellies_url",
5
+ :order => "created_at DESC"
6
+
7
+ end
@@ -0,0 +1,9 @@
1
+ class Jelly < ActiveRecord::Base
2
+
3
+ validates_presence_of :title
4
+ validates_presence_of :name
5
+
6
+ cattr_reader :per_page
7
+ @@per_page = 3
8
+
9
+ end
@@ -0,0 +1,8 @@
1
+ class PeanutButter < ActiveRecord::Base
2
+
3
+ validates_presence_of :viscosity
4
+
5
+ cattr_reader :per_page
6
+ @@per_page = 3
7
+
8
+ end
@@ -0,0 +1,4 @@
1
+ <p>
2
+ <%= form.label :viscosity %>
3
+ <%= form.text_field :viscosity %>
4
+ </p>
@@ -0,0 +1,11 @@
1
+ <h3>Edit Peanut Butter</h3>
2
+
3
+ <%= render 'shared/error_messages', :target => @peanut_butter %>
4
+
5
+ <%= form_for [:admin, @peanut_butter] do |f| %>
6
+ <%= render 'fields', :form => f %>
7
+ <p class="action">
8
+ <%= f.submit 'update' %>
9
+ or <%= link_to "cancel", admin_peanut_butters_path %>
10
+ </p>
11
+ <% end %>
@@ -0,0 +1,23 @@
1
+ <h3>Listing Peanut Butters</h3>
2
+ <%= link_to "<em>create</em> a new peanut_butter".html_safe, new_admin_peanut_butter_path %>
3
+
4
+ <% if @peanut_butters.empty? %>
5
+ to start your adventure.
6
+ <% else %>
7
+ or
8
+ <ul class="items">
9
+ <% @peanut_butters.each do |peanut_butter| %>
10
+ <li>
11
+ <span>
12
+ <%= link_to content_tag(:em, "read"), admin_peanut_butter_path(peanut_butter) %>,
13
+ <%= link_to content_tag(:em, "update"), edit_admin_peanut_butter_path(peanut_butter) %> or
14
+ <%= link_to content_tag(:em, "delete"), admin_peanut_butter_path(peanut_butter), :method => :delete, :confirm => "Are you sure?" %>
15
+ </span>
16
+ <b><%= peanut_butter.viscosity.inspect %></b>
17
+ </li>
18
+ <% end %>
19
+ </ul>
20
+ <%= will_paginate @peanut_butters %>
21
+ <br/>
22
+ <p>So much peanut_butter, <strike>so little time</strike> and time to eat it too.</p>
23
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <h3>New Peanut Butter</h3>
2
+
3
+ <%= render 'shared/error_messages', :target => @peanut_butter %>
4
+
5
+ <%= form_for [:admin, @peanut_butter] do |f| %>
6
+ <%= render 'fields', :form => f %>
7
+ <p class="action">
8
+ <%= f.submit 'create' %>
9
+ or <%= link_to "cancel", admin_peanut_butters_path %>
10
+ </p>
11
+ <% end %>
@@ -0,0 +1,14 @@
1
+ <h3>Show Peanut Butter</h3>
2
+
3
+ <%= render 'shared/error_messages', :target => @peanut_butter %>
4
+
5
+ <h3><%= @peanut_butter.viscosity %></h3>
6
+
7
+ <br/>
8
+
9
+ <p>
10
+ <%= link_to content_tag(:em, "update"), edit_admin_peanut_butter_path(@peanut_butter) %> or
11
+ <%= link_to content_tag(:em, "delete"), admin_peanut_butter_path(@peanut_butter), :method => :delete, :confirm => "Are you sure?" %>
12
+ this peanut_butter, or
13
+ <%= link_to "return to peanut_butters", admin_peanut_butters_path %>
14
+ </p>
@@ -0,0 +1,8 @@
1
+ <p>
2
+ <%= form.label :title %>
3
+ <%= form.text_field :title %>
4
+ </p>
5
+ <p>
6
+ <%= form.label :name %>
7
+ <%= form.text_field :name %>
8
+ </p>
@@ -0,0 +1,11 @@
1
+ <h3>Edit Jelly</h3>
2
+
3
+ <%= render 'shared/error_messages', :target => @jelly %>
4
+
5
+ <%= form_for @jelly do |f| %>
6
+ <%= render 'fields', :form => f %>
7
+ <p class="action">
8
+ <%= f.submit 'update' %>
9
+ or <%= link_to "cancel", jellies_path %>
10
+ </p>
11
+ <% end %>
@@ -0,0 +1,23 @@
1
+ <h3>Listing Jellies</h3>
2
+ <%= link_to "<em>create</em> a new jelly".html_safe, new_jelly_path %>
3
+
4
+ <% if @jellies.empty? %>
5
+ to start your adventure.
6
+ <% else %>
7
+ or
8
+ <ul class="items">
9
+ <% @jellies.each do |jelly| %>
10
+ <li>
11
+ <span>
12
+ <%= link_to content_tag(:em, "read"), jelly_path(jelly) %>,
13
+ <%= link_to content_tag(:em, "update"), edit_jelly_path(jelly) %> or
14
+ <%= link_to content_tag(:em, "delete"), jelly_path(jelly), :method => :delete, :confirm => "Are you sure?" %>
15
+ </span>
16
+ jelly named <b><%= jelly.name %></b>
17
+ </li>
18
+ <% end %>
19
+ </ul>
20
+ <%= will_paginate @jellies %>
21
+ <br/>
22
+ <p>So much jelly, <strike>so little time</strike> and time to eat it too.</p>
23
+ <% end %>
@@ -0,0 +1,11 @@
1
+ <h3>New Jelly</h3>
2
+
3
+ <%= render 'shared/error_messages', :target => @jelly %>
4
+
5
+ <%= form_for @jelly do |f| %>
6
+ <%= render 'fields', :form => f %>
7
+ <p class="action">
8
+ <%= f.submit 'create' %>
9
+ or <%= link_to "cancel", jellies_path %>
10
+ </p>
11
+ <% end %>
@@ -0,0 +1,15 @@
1
+ <h3>Show Jelly</h3>
2
+
3
+ <%= render 'shared/error_messages', :target => @jelly %>
4
+
5
+ <h3><%= @jelly.title %></h3>
6
+ <p><%= @jelly.name %></p>
7
+
8
+ <br/>
9
+
10
+ <p>
11
+ <%= link_to content_tag(:em, "update"), edit_jelly_path(@jelly) %> or
12
+ <%= link_to content_tag(:em, "delete"), jelly_path(@jelly), :method => :delete, :confirm => "Are you sure?" %>
13
+ this jelly, or
14
+ <%= link_to "return to jellies", jellies_path %>
15
+ </p>
@@ -0,0 +1,72 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Crudify Dummy</title>
5
+ <%= csrf_meta_tag %>
6
+ <%= stylesheet_link_tag 'styles' %>
7
+ <%= javascript_include_tag :defaults %>
8
+ </head>
9
+ <body>
10
+
11
+ <div id="container">
12
+
13
+ <div id="header">
14
+ <h1 class="title">Crudify</h1>
15
+ <h2>All the CRUD, no hassle.</h2>
16
+ </div>
17
+
18
+ <hr/>
19
+ <h4><em>Disclaimer</em></h4>
20
+ <p>Crudify was shamlessly robbed from <%= link_to "refinerycms", "https://github.com/resolve/refinerycms/blob/master/core/lib/refinery/crud.rb" %>'s internals and customized for use in other projects. Much credit and many thanks to the guys at <%= link_to "resolve digital", "http://resolvedigital.com/" %> for all their hard work on the <%= link_to "refinery cms project", "http://resolvedigital.com/development/refinery%C2%A0cms" %>. Keep it up!</p>
21
+
22
+ <hr/>
23
+ <h3>Here's the controller running this demo:</h3>
24
+
25
+ <% file = File.expand_path("../../../controllers/#{controller.controller_path}_controller.rb", __FILE__) %>
26
+ <% if File.exists?(file) %>
27
+ <pre><%= File.read(file) %></pre>
28
+ <% end %>
29
+
30
+ <hr/>
31
+ <h3>Here's the CRUD:</h3>
32
+
33
+ <%= content_tag(:p, flash[:notice], :id => 'flash_notice', :class => 'flash') if flash[:notice] %>
34
+ <%= content_tag(:p, flash[:error], :id => 'flash_error', :class => 'flash') if flash[:error] %>
35
+
36
+ <div id="content">
37
+ <%= yield %>
38
+ </div>
39
+
40
+
41
+ <h3>Here's some more info:</h3>
42
+
43
+ <p>Like the available options:</p>
44
+ <pre>
45
+ :title_attribute => "title",
46
+ :use_class_name_as_title => false,
47
+ :paginate => true,
48
+ :sortable => true,
49
+ :searchable => true,
50
+ :include => [],
51
+ :order => ('position ASC' if this_class.table_exists?
52
+ && this_class.column_names.include?('position')),
53
+ :conditions => '',
54
+ :search_conditions => '',
55
+ :redirect_to_url => "admin_#{plural_name}_url",
56
+ :log => Rails.env == 'development'</pre>
57
+
58
+
59
+
60
+ <hr/>
61
+ <h3>Get some:</h3>
62
+ <p class="small"><%= link_to "Github", "https://github.com/citrus/crudify" %> or <%= link_to "Ruby Gems", "http://rubygems.org/gems/crudify" %>; take your pick.</p>
63
+
64
+
65
+
66
+ <hr/>
67
+ <em>Now enjoy your jellies with no hassle from crud!</em>
68
+
69
+ </div>
70
+
71
+ </body>
72
+ </html>
@@ -0,0 +1,9 @@
1
+ <% target ||= nil %>
2
+ <% unless target.nil? || target.errors.to_a.empty? %>
3
+ <div class="flash">
4
+ <b>Errors:</b>
5
+ <ul class="errors">
6
+ <%= target.errors.to_a.collect{|e|"<li>#{e}</li>"}.join("\n").html_safe %>
7
+ </ul>
8
+ </div>
9
+ <% end %>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,45 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ require "action_mailer/railtie"
8
+
9
+ Bundler.require
10
+ require "crudify"
11
+
12
+ module Dummy
13
+ class Application < Rails::Application
14
+ # Settings in config/environments/* take precedence over those specified here.
15
+ # Application configuration should go into files in config/initializers
16
+ # -- all .rb files in that directory are automatically loaded.
17
+
18
+ # Custom directories with classes and modules you want to be autoloadable.
19
+ # config.autoload_paths += %W(#{config.root}/extras)
20
+
21
+ # Only load the plugins named here, in the order given (default is alphabetical).
22
+ # :all can be used as a placeholder for all plugins not explicitly named.
23
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
24
+
25
+ # Activate observers that should always be running.
26
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
27
+
28
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
29
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
30
+ # config.time_zone = 'Central Time (US & Canada)'
31
+
32
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
33
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
34
+ # config.i18n.default_locale = :de
35
+
36
+ # JavaScript files you want as :defaults (application.js is always included).
37
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
38
+
39
+ # Configure the default encoding used in templates for Ruby 1.9.
40
+ config.encoding = "utf-8"
41
+
42
+ # Configure sensitive parameters which will be filtered from the log file.
43
+ config.filter_parameters += [:password]
44
+ end
45
+ end