rails-crud 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/README ADDED
@@ -0,0 +1,51 @@
1
+ # What is rails-crud?
2
+
3
+ rails-crud is a plugin that abstracts a bunch of the repetitive CRUD actions out. Like scaffold, but I hate scaffold.
4
+
5
+ # Installation
6
+
7
+ Install it like a regular plugin.
8
+
9
+ # Usage
10
+
11
+ class Pastry < ActiveRecord::Base
12
+ validates_presence_of :name
13
+ end
14
+
15
+ class PastriesController
16
+ include CRUD::Base
17
+ end
18
+
19
+ Ta-da.
20
+
21
+ Really, that's it.
22
+
23
+ Your controller has to be the plural of your model. Has to.
24
+
25
+ You'll get the following actions:
26
+
27
+ * index
28
+ * new
29
+ * create
30
+ * edit
31
+ * update
32
+ * destroy
33
+
34
+ They're ugly as sin, but they'll work like you want, and they're easily stylable.
35
+
36
+ # Customizations
37
+
38
+ You can customize the following views:
39
+
40
+ * field layout on the forms
41
+ * column layout on index
42
+
43
+ You can customize the following behavior:
44
+
45
+ * some. seriously, this is new documentation. I'll get to it. Or look at the code. It's not terribly complicated. Fork, update the docs and send me a pull request
46
+
47
+ # Requirements:
48
+
49
+ * haml
50
+ * validation_reflection (not *really* required, but trust me, you'll want it.)
51
+ * formtastic
data/Rakefile ADDED
@@ -0,0 +1,29 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+ require 'rake/gempackagetask'
4
+
5
+ PKG_FILES = FileList[
6
+ '[a-zA-Z]*',
7
+ 'lib/**/*',
8
+ 'app/**/*',
9
+ 'rails/**/*'
10
+ ]
11
+
12
+ spec = Gem::Specification.new do |s|
13
+ s.name = 'rails-crud'
14
+ s.version = '0.0.1'
15
+ s.author = 'Jon Moses'
16
+ s.email = 'jon@burningbush.us'
17
+ s.homepage = 'http://github.com/jmoses/rails-crud'
18
+ s.platform = Gem::Platform::RUBY
19
+ s.summary = "Rails plugin for making CRUD easier"
20
+ s.files = PKG_FILES.to_a
21
+ s.require_path = 'lib'
22
+ s.has_rdoc = false
23
+ s.extra_rdoc_files = ["README"]
24
+ end
25
+
26
+ desc 'Build the gem'
27
+ Rake::GemPackageTask.new(spec) do |pkg|
28
+ pkg.gem_spec = spec
29
+ end
@@ -0,0 +1 @@
1
+ = form.inputs
@@ -0,0 +1,4 @@
1
+ - semantic_form_for object do |form|
2
+ = form.semantic_errors
3
+ = crud_fields(form)
4
+ = form.buttons
File without changes
@@ -0,0 +1,5 @@
1
+ %h1
2
+ Edit
3
+ = base_name.titleize
4
+
5
+ = default_form_for get_model_variable
@@ -0,0 +1,24 @@
1
+ %h1= base_name.pluralize.titleize
2
+
3
+ = link_to "new", new_polymorphic_path(model)
4
+
5
+ - if get_collection_variable.empty?
6
+ %h2 Nothing found
7
+ - else
8
+ = will_paginate get_collection_variable
9
+ %table
10
+ %thead
11
+ %tr
12
+ - content_columns.each do |column|
13
+ %th= header_for_column column
14
+ %th Actions
15
+ %tbody
16
+ - get_collection_variable.each do |obj|
17
+ %tr
18
+ - content_columns.each do |column|
19
+ %td= value_for_column(obj, column)
20
+ %td
21
+ = link_to 'edit', edit_polymorphic_path(obj)
22
+ = link_to 'destroy', polymorphic_path(obj), :method => :delete, :confirm => "Are you sure?"
23
+ = other_actions( obj )
24
+ = will_paginate get_collection_variable
@@ -0,0 +1,5 @@
1
+ %h1
2
+ New
3
+ = base_name.titleize
4
+
5
+ = default_form_for get_model_variable
data/lib/crud.rb ADDED
@@ -0,0 +1,142 @@
1
+ module CRUD
2
+ module Base
3
+ def self.included(base)
4
+ base.send :include, ClassMethods
5
+ end
6
+
7
+ module ClassMethods
8
+ def self.included(base)
9
+ base.class_eval do
10
+ include ActiveSupport::Callbacks
11
+
12
+ before_filter :build_object, :only => [:new, :create]
13
+ before_filter :load_object, :only => [:edit, :update, :destroy]
14
+ before_filter :load_collection, :only => :index
15
+
16
+ helper_method :base_name, :model, :get_model_variable, :get_collection_variable, :header_for_column,
17
+ :content_columns, :value_for_column
18
+
19
+ define_callbacks :before_save
20
+ end
21
+ end
22
+ end
23
+
24
+ def index;
25
+ crud_render
26
+ end
27
+ def new;
28
+ crud_render
29
+ end
30
+ def edit
31
+ crud_render
32
+ end
33
+
34
+ def create
35
+ run_callbacks(:before_save)
36
+
37
+ if get_model_variable.save
38
+ redirect_to :action => :index and return
39
+ else
40
+ crud_render :new
41
+ end
42
+ end
43
+
44
+ def update
45
+ get_model_variable.attributes = params[param_key]
46
+
47
+ run_callbacks(:before_save)
48
+
49
+ if get_model_variable.save
50
+ redirect_to :action => :index and return
51
+ else
52
+ crud_render :edit
53
+ end
54
+ end
55
+
56
+ def destroy
57
+ if get_model_variable.destroy
58
+ redirect_to :action => :index and return
59
+ else
60
+ crud_render :index
61
+ end
62
+ end
63
+
64
+ protected
65
+ def content_columns
66
+ [
67
+ [:to_s, "Object"]
68
+ ]
69
+ end
70
+
71
+ def header_for_column( col )
72
+ col[1] or col[0].to_s.humanize.titleize
73
+ end
74
+
75
+ def value_for_column( object, col )
76
+ case ( thing = col.first)
77
+ when Symbol
78
+ object.send thing
79
+ when Proc
80
+ thing.call( object )
81
+ else
82
+ thing
83
+ end
84
+ end
85
+
86
+ def build_object
87
+ set_model_variable model.new( params[param_key] )
88
+ end
89
+
90
+ def load_object
91
+ set_model_variable model.find( params[:id] )
92
+ end
93
+
94
+ def load_collection
95
+ set_collection_variable model.paginate :page => params[:page], :order => collection_order
96
+ end
97
+
98
+ def collection_order
99
+ "created_at asc"
100
+ end
101
+
102
+ def set_collection_variable( obj )
103
+ instance_variable_set("@#{base_name.pluralize}", obj)
104
+ end
105
+
106
+ def get_collection_variable
107
+ instance_variable_get("@#{base_name.pluralize}")
108
+ end
109
+
110
+ def base_name
111
+ @base_name ||= controller_name.singularize
112
+ end
113
+
114
+ def param_key
115
+ base_name.to_sym
116
+ end
117
+
118
+ def model_variable
119
+ "@#{base_name}"
120
+ end
121
+
122
+ def set_model_variable( obj )
123
+ instance_variable_set(model_variable, obj)
124
+ end
125
+
126
+ def get_model_variable
127
+ instance_variable_get(model_variable)
128
+ end
129
+
130
+ def model
131
+ base_name.classify.constantize
132
+ end
133
+
134
+ def crud_render( template = action_name )
135
+ begin
136
+ render template
137
+ rescue ActionView::MissingTemplate
138
+ render :template => "crud/#{template}"
139
+ end
140
+ end
141
+ end
142
+ end
@@ -0,0 +1,23 @@
1
+ module CRUD
2
+ module CrudHelper
3
+ def default_form_for( object )
4
+ crud_partial "form", :object => object
5
+ end
6
+
7
+ def crud_fields( form )
8
+ crud_partial "fields", :form => form
9
+ end
10
+
11
+ def crud_partial( partial, locals = {} )
12
+ begin
13
+ render :partial => partial, :locals => locals
14
+ rescue ActionView::MissingTemplate
15
+ render :partial => "crud/#{partial}", :locals => locals
16
+ end
17
+ end
18
+
19
+ def other_actions( object )
20
+ crud_partial "other_actions", :object => object
21
+ end
22
+ end
23
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,4 @@
1
+ require 'crud'
2
+ require 'crud_helper'
3
+
4
+ ActionView::Base.send :include, CRUD::CrudHelper
metadata ADDED
@@ -0,0 +1,65 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rails-crud
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jon Moses
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2010-02-04 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description:
17
+ email: jon@burningbush.us
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README
24
+ files:
25
+ - Rakefile
26
+ - README
27
+ - lib/crud.rb
28
+ - lib/crud_helper.rb
29
+ - app/views/crud/_fields.html.haml
30
+ - app/views/crud/_form.html.haml
31
+ - app/views/crud/_other_actions.html.haml
32
+ - app/views/crud/edit.html.haml
33
+ - app/views/crud/index.html.haml
34
+ - app/views/crud/new.html.haml
35
+ - rails/init.rb
36
+ has_rdoc: true
37
+ homepage: http://github.com/jmoses/rails-crud
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.3.5
61
+ signing_key:
62
+ specification_version: 3
63
+ summary: Rails plugin for making CRUD easier
64
+ test_files: []
65
+