crudify 0.0.1
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/.gitignore +5 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +94 -0
- data/README.md +17 -0
- data/Rakefile +13 -0
- data/config/locales/en.yml +8 -0
- data/crudify.gemspec +26 -0
- data/lib/crudify.rb +12 -0
- data/lib/crudify/base.rb +42 -0
- data/lib/crudify/class_methods.rb +242 -0
- data/lib/crudify/hook_methods.rb +120 -0
- data/lib/crudify/version.rb +3 -0
- data/log/development.log +0 -0
- data/test/helper.rb +40 -0
- data/test/test_controller.rb +57 -0
- metadata +139 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,94 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
crudify (0.0.1)
|
5
|
+
meta_search (>= 1.0.1)
|
6
|
+
rails (>= 3.0.0)
|
7
|
+
will_paginate (>= 3.0.pre2)
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
abstract (1.0.0)
|
13
|
+
actionmailer (3.0.3)
|
14
|
+
actionpack (= 3.0.3)
|
15
|
+
mail (~> 2.2.9)
|
16
|
+
actionpack (3.0.3)
|
17
|
+
activemodel (= 3.0.3)
|
18
|
+
activesupport (= 3.0.3)
|
19
|
+
builder (~> 2.1.2)
|
20
|
+
erubis (~> 2.6.6)
|
21
|
+
i18n (~> 0.4)
|
22
|
+
rack (~> 1.2.1)
|
23
|
+
rack-mount (~> 0.6.13)
|
24
|
+
rack-test (~> 0.5.6)
|
25
|
+
tzinfo (~> 0.3.23)
|
26
|
+
activemodel (3.0.3)
|
27
|
+
activesupport (= 3.0.3)
|
28
|
+
builder (~> 2.1.2)
|
29
|
+
i18n (~> 0.4)
|
30
|
+
activerecord (3.0.3)
|
31
|
+
activemodel (= 3.0.3)
|
32
|
+
activesupport (= 3.0.3)
|
33
|
+
arel (~> 2.0.2)
|
34
|
+
tzinfo (~> 0.3.23)
|
35
|
+
activeresource (3.0.3)
|
36
|
+
activemodel (= 3.0.3)
|
37
|
+
activesupport (= 3.0.3)
|
38
|
+
activesupport (3.0.3)
|
39
|
+
arel (2.0.7)
|
40
|
+
builder (2.1.2)
|
41
|
+
erubis (2.6.6)
|
42
|
+
abstract (>= 1.0.0)
|
43
|
+
i18n (0.5.0)
|
44
|
+
mail (2.2.14)
|
45
|
+
activesupport (>= 2.3.6)
|
46
|
+
i18n (>= 0.4.0)
|
47
|
+
mime-types (~> 1.16)
|
48
|
+
treetop (~> 1.4.8)
|
49
|
+
meta_search (1.0.1)
|
50
|
+
actionpack (~> 3.0.2)
|
51
|
+
activerecord (~> 3.0.2)
|
52
|
+
activesupport (~> 3.0.2)
|
53
|
+
arel (~> 2.0.2)
|
54
|
+
mime-types (1.16)
|
55
|
+
polyglot (0.3.1)
|
56
|
+
rack (1.2.1)
|
57
|
+
rack-mount (0.6.13)
|
58
|
+
rack (>= 1.0.0)
|
59
|
+
rack-test (0.5.7)
|
60
|
+
rack (>= 1.0)
|
61
|
+
rails (3.0.3)
|
62
|
+
actionmailer (= 3.0.3)
|
63
|
+
actionpack (= 3.0.3)
|
64
|
+
activerecord (= 3.0.3)
|
65
|
+
activeresource (= 3.0.3)
|
66
|
+
activesupport (= 3.0.3)
|
67
|
+
bundler (~> 1.0)
|
68
|
+
railties (= 3.0.3)
|
69
|
+
railties (3.0.3)
|
70
|
+
actionpack (= 3.0.3)
|
71
|
+
activesupport (= 3.0.3)
|
72
|
+
rake (>= 0.8.7)
|
73
|
+
thor (~> 0.14.4)
|
74
|
+
rake (0.8.7)
|
75
|
+
shoulda (2.11.3)
|
76
|
+
sqlite3 (1.3.3)
|
77
|
+
sqlite3-ruby (1.3.3)
|
78
|
+
sqlite3 (>= 1.3.3)
|
79
|
+
thor (0.14.6)
|
80
|
+
treetop (1.4.9)
|
81
|
+
polyglot (>= 0.3.1)
|
82
|
+
tzinfo (0.3.24)
|
83
|
+
will_paginate (3.0.pre2)
|
84
|
+
|
85
|
+
PLATFORMS
|
86
|
+
ruby
|
87
|
+
|
88
|
+
DEPENDENCIES
|
89
|
+
crudify!
|
90
|
+
meta_search (>= 1.0.1)
|
91
|
+
rails (>= 3.0.0)
|
92
|
+
shoulda (>= 2.11.3)
|
93
|
+
sqlite3-ruby (>= 1.3.3)
|
94
|
+
will_paginate (>= 3.0.pre2)
|
data/README.md
ADDED
data/Rakefile
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
en:
|
2
|
+
crudify:
|
3
|
+
created: %{what} was successfully created.
|
4
|
+
updated: %{what} was successfully updated.
|
5
|
+
destroyed: %{what} was successfully removed.
|
6
|
+
failed_create: %{what} could not be created.
|
7
|
+
failed_update: %{what} could not be updated.
|
8
|
+
failed_destroy: %{what} could not be removed.
|
data/crudify.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "crudify/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "crudify"
|
7
|
+
s.version = Crudify::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Spencer Steffen"]
|
10
|
+
s.email = ["spencer@citrusme.com"]
|
11
|
+
s.homepage = "http://github.com/citrus/crudify"
|
12
|
+
|
13
|
+
s.summary = %q{Crudify is a dynamic resource controller for Rails 3.}
|
14
|
+
s.description = %q{Crudify is a dynamic resource controller for Rails 3. The goal is to have very skinny controllers with powerful hooks to easily customize.}
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
18
|
+
s.require_paths = ["lib"]
|
19
|
+
|
20
|
+
s.add_dependency('rails', '>= 3.0.0')
|
21
|
+
# s.add_dependency('will_paginate', '>= ')
|
22
|
+
s.add_dependency('meta_search', '>= 1.0.1')
|
23
|
+
|
24
|
+
s.add_development_dependency('shoulda', '>= 2.11.3')
|
25
|
+
s.add_development_dependency('sqlite3-ruby', '>= 1.3.3')
|
26
|
+
end
|
data/lib/crudify.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'active_record'
|
3
|
+
require 'action_controller'
|
4
|
+
|
5
|
+
require 'crudify/hook_methods'
|
6
|
+
require 'crudify/class_methods'
|
7
|
+
require 'crudify/base'
|
8
|
+
|
9
|
+
module Crudify
|
10
|
+
end
|
11
|
+
|
12
|
+
ActionController::Base.send(:include, Crudify::Base)
|
data/lib/crudify/base.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# Base methods for CRUD actions
|
2
|
+
# Simply override any methods in your action controller you want to be customised
|
3
|
+
# Don't forget to add:
|
4
|
+
# resources :plural_model_name_here
|
5
|
+
# to your routes.rb file.
|
6
|
+
# Full documentation about CRUD and resources go here:
|
7
|
+
# -> http://caboo.se/doc/classes/ActionController/Resources.html#M003716
|
8
|
+
# Example (add to your controller):
|
9
|
+
# crudify :foo, {:title_attribute => 'name'}
|
10
|
+
|
11
|
+
|
12
|
+
module Crudify
|
13
|
+
|
14
|
+
module Base
|
15
|
+
|
16
|
+
def self.default_options(model_name)
|
17
|
+
singular_name = model_name.to_s
|
18
|
+
class_name = singular_name.camelize
|
19
|
+
plural_name = singular_name.pluralize
|
20
|
+
this_class = class_name.constantize
|
21
|
+
{
|
22
|
+
:title_attribute => "title",
|
23
|
+
:order => ('position ASC' if this_class.table_exists? and this_class.column_names.include?('position')),
|
24
|
+
:conditions => '',
|
25
|
+
:sortable => true,
|
26
|
+
:searchable => true,
|
27
|
+
:include => [],
|
28
|
+
:paging => true,
|
29
|
+
:search_conditions => '',
|
30
|
+
:redirect_to_url => "admin_#{plural_name}_url"
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.append_features(base)
|
35
|
+
super
|
36
|
+
base.send(:include, Crudify::HookMethods)
|
37
|
+
base.extend(Crudify::ClassMethods)
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
|
42
|
+
end
|
@@ -0,0 +1,242 @@
|
|
1
|
+
module Crudify
|
2
|
+
|
3
|
+
module ClassMethods
|
4
|
+
|
5
|
+
def crudify(model_name, options = {})
|
6
|
+
options = ::Crudify::Base.default_options(model_name).merge(options)
|
7
|
+
|
8
|
+
singular_name = model_name.to_s
|
9
|
+
class_name = singular_name.camelize
|
10
|
+
plural_name = singular_name.pluralize
|
11
|
+
|
12
|
+
options[:paging] = (options[:paging] && eval(class_name).respond_to?(:paginate))
|
13
|
+
|
14
|
+
module_eval %(
|
15
|
+
|
16
|
+
# (Just a comment!)
|
17
|
+
|
18
|
+
prepend_before_filter :find_#{singular_name},
|
19
|
+
:only => [:update, :destroy, :edit, :show]
|
20
|
+
|
21
|
+
prepend_before_filter :set_crud_options
|
22
|
+
|
23
|
+
def set_crud_options
|
24
|
+
@crud_options = #{options}
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
def new
|
29
|
+
@#{singular_name} = #{class_name}.new
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
def create
|
34
|
+
# if the position field exists, set this object as last object, given the conditions of this class.
|
35
|
+
if #{class_name}.column_names.include?("position")
|
36
|
+
params[:#{singular_name}].merge!({
|
37
|
+
:position => ((#{class_name}.maximum(:position, :conditions => #{options[:conditions].inspect})||-1) + 1)
|
38
|
+
})
|
39
|
+
end
|
40
|
+
@instance = @#{singular_name} = #{class_name}.create(params[:#{singular_name}])
|
41
|
+
before_create
|
42
|
+
if @instance.valid? && @instance.save
|
43
|
+
successful_create
|
44
|
+
else
|
45
|
+
failed_create
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def edit
|
50
|
+
# object gets found by find_#{singular_name} function
|
51
|
+
end
|
52
|
+
|
53
|
+
def update
|
54
|
+
before_update
|
55
|
+
if @#{singular_name}.update_attributes(params[:#{singular_name}])
|
56
|
+
successful_update
|
57
|
+
else
|
58
|
+
failed_update
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def destroy
|
63
|
+
before_destroy
|
64
|
+
# object gets found by find_#{singular_name} function
|
65
|
+
title = @#{singular_name}.#{options[:title_attribute]}
|
66
|
+
if @#{singular_name}.destroy
|
67
|
+
successful_destroy
|
68
|
+
else
|
69
|
+
failed_destroy
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
def set_instance(record)
|
77
|
+
@instance = @#{singular_name} = record
|
78
|
+
end
|
79
|
+
|
80
|
+
def set_collection(scope, with_options=true)
|
81
|
+
@collection = @#{plural_name} = with_options ? scope_with_options(scope) : scope
|
82
|
+
end
|
83
|
+
|
84
|
+
def scope_with_options(scope)
|
85
|
+
scope.includes(
|
86
|
+
#{options[:include].map(&:to_sym).inspect}
|
87
|
+
).order("#{options[:order]}")
|
88
|
+
end
|
89
|
+
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
# Finds one single result based on the id params.
|
94
|
+
def find_#{singular_name}
|
95
|
+
set_instance(#{class_name}.find(params[:id],
|
96
|
+
:include => #{options[:include].map(&:to_sym).inspect}))
|
97
|
+
end
|
98
|
+
|
99
|
+
|
100
|
+
# Find the collection of @#{plural_name} based on the conditions specified into crudify
|
101
|
+
# It will be ordered based on the conditions specified into crudify
|
102
|
+
# And eager loading is applied as specified into crudify.
|
103
|
+
def find_all_#{plural_name}(conditions = #{options[:conditions].inspect})
|
104
|
+
set_collection(#{class_name}.where(conditions))
|
105
|
+
end
|
106
|
+
|
107
|
+
|
108
|
+
# Paginate a set of @#{plural_name} that may/may not already exist.
|
109
|
+
def paginate_all_#{plural_name}
|
110
|
+
# If we have already found a set then we don't need to again
|
111
|
+
find_all_#{plural_name} if @#{plural_name}.nil?
|
112
|
+
|
113
|
+
paging_options = {:page => params[:page]}
|
114
|
+
|
115
|
+
# Seems will_paginate doesn't always use the implicit method.
|
116
|
+
if #{class_name}.methods.map(&:to_sym).include?(:per_page)
|
117
|
+
paging_options.update({:per_page => #{class_name}.per_page})
|
118
|
+
end
|
119
|
+
|
120
|
+
set_collection(@#{plural_name}.paginate(paging_options), false)
|
121
|
+
end
|
122
|
+
|
123
|
+
# Returns results based on the query specified by the user.
|
124
|
+
def search_all_#{plural_name}
|
125
|
+
@search ||= find_all_#{plural_name}.search(params[:search])
|
126
|
+
set_collection(@search, false)
|
127
|
+
end
|
128
|
+
|
129
|
+
|
130
|
+
# Ensure all methods are protected so that they should only be called
|
131
|
+
# from within the current controller.
|
132
|
+
protected :find_#{singular_name},
|
133
|
+
:find_all_#{plural_name},
|
134
|
+
:paginate_all_#{plural_name},
|
135
|
+
:search_all_#{plural_name}
|
136
|
+
)
|
137
|
+
|
138
|
+
# Methods that are only included when this controller is searchable.
|
139
|
+
if options[:searchable]
|
140
|
+
if options[:paging]
|
141
|
+
module_eval %(
|
142
|
+
def index
|
143
|
+
search_all_#{plural_name} if searching?
|
144
|
+
paginate_all_#{plural_name}
|
145
|
+
end
|
146
|
+
)
|
147
|
+
else
|
148
|
+
module_eval %(
|
149
|
+
def index
|
150
|
+
unless searching?
|
151
|
+
find_all_#{plural_name}
|
152
|
+
else
|
153
|
+
search_all_#{plural_name}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
)
|
157
|
+
end
|
158
|
+
else
|
159
|
+
if options[:paging]
|
160
|
+
module_eval %(
|
161
|
+
def index
|
162
|
+
paginate_all_#{plural_name}
|
163
|
+
end
|
164
|
+
)
|
165
|
+
else
|
166
|
+
module_eval %(
|
167
|
+
def index
|
168
|
+
find_all_#{plural_name}
|
169
|
+
end
|
170
|
+
)
|
171
|
+
end
|
172
|
+
|
173
|
+
end
|
174
|
+
|
175
|
+
if options[:sortable]
|
176
|
+
module_eval %(
|
177
|
+
def reorder
|
178
|
+
find_all_#{plural_name}
|
179
|
+
end
|
180
|
+
|
181
|
+
# Based upon http://github.com/matenia/jQuery-Awesome-Nested-Set-Drag-and-Drop
|
182
|
+
def update_positions
|
183
|
+
previous = nil
|
184
|
+
# The list doesn't come to us in the correct order. Frustration.
|
185
|
+
0.upto((newlist ||= params[:ul]).length - 1) do |index|
|
186
|
+
hash = newlist[index.to_s]
|
187
|
+
moved_item_id = hash['id'].split(/#{singular_name}\\_?/)
|
188
|
+
@current_#{singular_name} = #{class_name}.find_by_id(moved_item_id)
|
189
|
+
|
190
|
+
if @current_#{singular_name}.respond_to?(:move_to_root)
|
191
|
+
if previous.present?
|
192
|
+
@current_#{singular_name}.move_to_right_of(#{class_name}.find_by_id(previous))
|
193
|
+
else
|
194
|
+
@current_#{singular_name}.move_to_root
|
195
|
+
end
|
196
|
+
else
|
197
|
+
@current_#{singular_name}.update_attribute(:position, index)
|
198
|
+
end
|
199
|
+
|
200
|
+
if hash['children'].present?
|
201
|
+
update_child_positions(hash, @current_#{singular_name})
|
202
|
+
end
|
203
|
+
|
204
|
+
previous = moved_item_id
|
205
|
+
end
|
206
|
+
|
207
|
+
#{class_name}.rebuild! if #{class_name}.respond_to?(:rebuild!)
|
208
|
+
render :nothing => true
|
209
|
+
end
|
210
|
+
|
211
|
+
def update_child_positions(node, #{singular_name})
|
212
|
+
0.upto(node['children'].length - 1) do |child_index|
|
213
|
+
child = node['children'][child_index.to_s]
|
214
|
+
child_id = child['id'].split(/#{singular_name}\_?/)
|
215
|
+
child_#{singular_name} = #{class_name}.find_by_id(child_id)
|
216
|
+
child_#{singular_name}.move_to_child_of(#{singular_name})
|
217
|
+
|
218
|
+
if child['children'].present?
|
219
|
+
update_child_positions(child, child_#{singular_name})
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
|
224
|
+
)
|
225
|
+
end
|
226
|
+
|
227
|
+
module_eval %(
|
228
|
+
def self.sortable?
|
229
|
+
#{options[:sortable].to_s}
|
230
|
+
end
|
231
|
+
|
232
|
+
def self.searchable?
|
233
|
+
#{options[:searchable].to_s}
|
234
|
+
end
|
235
|
+
)
|
236
|
+
|
237
|
+
|
238
|
+
end
|
239
|
+
|
240
|
+
end
|
241
|
+
|
242
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module Crudify
|
2
|
+
|
3
|
+
module HookMethods
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def before_create
|
8
|
+
# just a hook!
|
9
|
+
puts "> Crud::before_create"
|
10
|
+
before_action
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_update
|
14
|
+
# just a hook!
|
15
|
+
puts "> Crud::before_update"
|
16
|
+
before_action
|
17
|
+
end
|
18
|
+
|
19
|
+
def before_destroy
|
20
|
+
# just a hook!
|
21
|
+
puts "> Crud::before_destroy"
|
22
|
+
before_action
|
23
|
+
end
|
24
|
+
|
25
|
+
|
26
|
+
def before_action
|
27
|
+
# just a hook!
|
28
|
+
puts "> Crud::before_action"
|
29
|
+
@what = @instance.send(@crud_options[:title_attribute].to_sym).inspect
|
30
|
+
true
|
31
|
+
end
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def successful_create
|
36
|
+
puts "> Crud::successful_create"
|
37
|
+
|
38
|
+
flash[:notice] = t('crudify.created', :what => @what)
|
39
|
+
|
40
|
+
after_success
|
41
|
+
end
|
42
|
+
|
43
|
+
def successful_update
|
44
|
+
puts "> Crud::successful_update"
|
45
|
+
|
46
|
+
flash[:notice] = t('crudify.updated', :what => @what)
|
47
|
+
|
48
|
+
after_success
|
49
|
+
end
|
50
|
+
|
51
|
+
def successful_destroy
|
52
|
+
puts "> Crud::successful_destroy"
|
53
|
+
|
54
|
+
flash.notice = t('crudify.destroyed', :what => @what)
|
55
|
+
|
56
|
+
after_success
|
57
|
+
end
|
58
|
+
|
59
|
+
def after_success
|
60
|
+
puts "> Crud::after_success"
|
61
|
+
|
62
|
+
unless request.xhr?
|
63
|
+
if params[:commit].match(/continue/)
|
64
|
+
if params[:action] == 'create'
|
65
|
+
redirect_to request.referer.sub('new', "#{what.to_param}/edit")
|
66
|
+
else
|
67
|
+
redirect_to request.referer
|
68
|
+
end
|
69
|
+
else
|
70
|
+
redirect_back_or_default eval(@crud_options[:redirect_to_url])
|
71
|
+
end
|
72
|
+
else
|
73
|
+
render :partial => "/shared/message"
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
|
80
|
+
|
81
|
+
def failed_create
|
82
|
+
puts "> Crud::failed_create"
|
83
|
+
|
84
|
+
flash[:error] = t('crudify.failed_create', :what => @what)
|
85
|
+
|
86
|
+
after_fail
|
87
|
+
end
|
88
|
+
|
89
|
+
def failed_update
|
90
|
+
puts "> Crud::failed_update"
|
91
|
+
|
92
|
+
flash[:error] = t('crudify.failed_update', :what => @what)
|
93
|
+
|
94
|
+
after_fail
|
95
|
+
end
|
96
|
+
|
97
|
+
def failed_destroy
|
98
|
+
puts "> Crud::failed_destroy"
|
99
|
+
|
100
|
+
flash[:error] = t('crudify.failed_destroy', :what => @what)
|
101
|
+
|
102
|
+
after_fail
|
103
|
+
end
|
104
|
+
|
105
|
+
|
106
|
+
def after_fail
|
107
|
+
puts "> Crud::after_fail"
|
108
|
+
|
109
|
+
unless request.xhr?
|
110
|
+
render :action => request.post? ? 'new' : 'edit'
|
111
|
+
else
|
112
|
+
flash[:error] = [flash[:error], what.errors.collect{|key,value| "#{key} #{value}"}.join("<br/>")]
|
113
|
+
render :partial => "/shared/message"
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
|
118
|
+
end
|
119
|
+
|
120
|
+
end
|
data/log/development.log
ADDED
File without changes
|
data/test/helper.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
begin
|
2
|
+
require 'rubygems'
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'rails/all'
|
5
|
+
require 'shoulda'
|
6
|
+
require 'crudify'
|
7
|
+
require 'will_paginate'
|
8
|
+
#require 'sqlite3'
|
9
|
+
rescue LoadError => e
|
10
|
+
puts "Load error!"
|
11
|
+
puts e.inspect
|
12
|
+
exit
|
13
|
+
end
|
14
|
+
|
15
|
+
|
16
|
+
class Jelly < ActiveRecord::Base
|
17
|
+
cattr_reader :per_page
|
18
|
+
@@per_page = 2
|
19
|
+
end
|
20
|
+
|
21
|
+
db = File.expand_path('../db/test.sqlite3', __FILE__)
|
22
|
+
`rm #{db}` if File.exists?(db)
|
23
|
+
|
24
|
+
config = { 'adapter' => 'sqlite3', 'database' => 'test/db/test.sqlite3' }
|
25
|
+
sql = 'CREATE TABLE "jellies" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "name" varchar(255), "created_at" datetime, "updated_at" datetime)'
|
26
|
+
ActiveRecord::Base.establish_connection(config)
|
27
|
+
ActiveRecord::Base.connection.execute(sql)
|
28
|
+
|
29
|
+
|
30
|
+
["blackberry", "blueberry", "strawberry"].each do |jelly|
|
31
|
+
Jelly.create(:title => "#{jelly.capitalize} Jelly", :name => jelly)
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
class CrudifyTestController < ActionController::Base
|
36
|
+
|
37
|
+
crudify :jelly
|
38
|
+
|
39
|
+
end
|
40
|
+
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require_relative 'helper'
|
2
|
+
|
3
|
+
|
4
|
+
def assert_has_method?(methods, name)
|
5
|
+
|
6
|
+
end
|
7
|
+
|
8
|
+
|
9
|
+
class TestController < Test::Unit::TestCase
|
10
|
+
|
11
|
+
def setup
|
12
|
+
@controller = CrudifyTestController.new
|
13
|
+
@controller.params = {}
|
14
|
+
end
|
15
|
+
|
16
|
+
should "have crud methods" do
|
17
|
+
[
|
18
|
+
:find_jelly,
|
19
|
+
:find_all_jellies,
|
20
|
+
:paginate_all_jellies,
|
21
|
+
:search_all_jellies
|
22
|
+
].each do |method|
|
23
|
+
assert @controller.methods.include?(method), "Should have #{method}"
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
should "have private hook methods" do
|
29
|
+
[
|
30
|
+
:before_create,
|
31
|
+
:before_update,
|
32
|
+
:successful_create,
|
33
|
+
:successful_update,
|
34
|
+
:successful_destroy,
|
35
|
+
:after_success,
|
36
|
+
:failed_create,
|
37
|
+
:failed_update,
|
38
|
+
:failed_destroy,
|
39
|
+
:after_fail
|
40
|
+
].each do |method|
|
41
|
+
assert @controller.private_methods.include?(method), "Should have #{method}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
should "get collection" do
|
46
|
+
jellies = @controller.send(:find_all_jellies)
|
47
|
+
assert_equal 3, jellies.length
|
48
|
+
end
|
49
|
+
|
50
|
+
#should "paginate collection" do
|
51
|
+
# jellies = @controller.send(:paginate_all_jellies)
|
52
|
+
# assert_equal 2, jellies.total_pages
|
53
|
+
# assert_equal 3, jellies.total_entries
|
54
|
+
#end
|
55
|
+
|
56
|
+
|
57
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crudify
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
version: 0.0.1
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Spencer Steffen
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2011-01-25 00:00:00 -08:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ">="
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 3
|
30
|
+
- 0
|
31
|
+
- 0
|
32
|
+
version: 3.0.0
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
name: meta_search
|
37
|
+
prerelease: false
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 1
|
47
|
+
version: 1.0.1
|
48
|
+
type: :runtime
|
49
|
+
version_requirements: *id002
|
50
|
+
- !ruby/object:Gem::Dependency
|
51
|
+
name: shoulda
|
52
|
+
prerelease: false
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 2
|
60
|
+
- 11
|
61
|
+
- 3
|
62
|
+
version: 2.11.3
|
63
|
+
type: :development
|
64
|
+
version_requirements: *id003
|
65
|
+
- !ruby/object:Gem::Dependency
|
66
|
+
name: sqlite3-ruby
|
67
|
+
prerelease: false
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ">="
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 3
|
76
|
+
- 3
|
77
|
+
version: 1.3.3
|
78
|
+
type: :development
|
79
|
+
version_requirements: *id004
|
80
|
+
description: Crudify is a dynamic resource controller for Rails 3. The goal is to have very skinny controllers with powerful hooks to easily customize.
|
81
|
+
email:
|
82
|
+
- spencer@citrusme.com
|
83
|
+
executables: []
|
84
|
+
|
85
|
+
extensions: []
|
86
|
+
|
87
|
+
extra_rdoc_files: []
|
88
|
+
|
89
|
+
files:
|
90
|
+
- .gitignore
|
91
|
+
- Gemfile
|
92
|
+
- Gemfile.lock
|
93
|
+
- README.md
|
94
|
+
- Rakefile
|
95
|
+
- config/locales/en.yml
|
96
|
+
- crudify.gemspec
|
97
|
+
- lib/crudify.rb
|
98
|
+
- lib/crudify/base.rb
|
99
|
+
- lib/crudify/class_methods.rb
|
100
|
+
- lib/crudify/hook_methods.rb
|
101
|
+
- lib/crudify/version.rb
|
102
|
+
- log/development.log
|
103
|
+
- test/helper.rb
|
104
|
+
- test/test_controller.rb
|
105
|
+
has_rdoc: true
|
106
|
+
homepage: http://github.com/citrus/crudify
|
107
|
+
licenses: []
|
108
|
+
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
|
112
|
+
require_paths:
|
113
|
+
- lib
|
114
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
115
|
+
none: false
|
116
|
+
requirements:
|
117
|
+
- - ">="
|
118
|
+
- !ruby/object:Gem::Version
|
119
|
+
segments:
|
120
|
+
- 0
|
121
|
+
version: "0"
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
none: false
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
segments:
|
128
|
+
- 0
|
129
|
+
version: "0"
|
130
|
+
requirements: []
|
131
|
+
|
132
|
+
rubyforge_project:
|
133
|
+
rubygems_version: 1.3.7
|
134
|
+
signing_key:
|
135
|
+
specification_version: 3
|
136
|
+
summary: Crudify is a dynamic resource controller for Rails 3.
|
137
|
+
test_files:
|
138
|
+
- test/helper.rb
|
139
|
+
- test/test_controller.rb
|