ao_crudify 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +7 -0
- data/Gemfile +8 -0
- data/LICENSE.md +21 -0
- data/README.md +93 -0
- data/Rakefile +11 -0
- data/ao_crudify.gemspec +26 -0
- data/config/locales/en.yml +8 -0
- data/config/locales/zh-cn.yml +8 -0
- data/lib/ao_crudify.rb +20 -0
- data/lib/ao_crudify/base.rb +43 -0
- data/lib/ao_crudify/class_methods.rb +137 -0
- data/lib/ao_crudify/hook_methods.rb +117 -0
- data/lib/ao_crudify/version.rb +3 -0
- data/spec/controllers/controller_hooks_spec.rb +1 -0
- data/spec/controllers/jellies_controller_spec.rb +156 -0
- data/spec/dummy-2-3-12/README +243 -0
- data/spec/dummy-2-3-12/Rakefile +10 -0
- data/spec/dummy-2-3-12/app/controllers/application_controller.rb +10 -0
- data/spec/dummy-2-3-12/app/controllers/jellies_controller.rb +6 -0
- data/spec/dummy-2-3-12/app/helpers/application_helper.rb +3 -0
- data/spec/dummy-2-3-12/app/models/jelly.rb +13 -0
- data/spec/dummy-2-3-12/config/boot.rb +114 -0
- data/spec/dummy-2-3-12/config/database.yml +22 -0
- data/spec/dummy-2-3-12/config/environment.rb +50 -0
- data/spec/dummy-2-3-12/config/environments/development.rb +17 -0
- data/spec/dummy-2-3-12/config/environments/production.rb +28 -0
- data/spec/dummy-2-3-12/config/environments/test.rb +28 -0
- data/spec/dummy-2-3-12/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy-2-3-12/config/initializers/cookie_verification_secret.rb +7 -0
- data/spec/dummy-2-3-12/config/initializers/inflections.rb +10 -0
- data/spec/dummy-2-3-12/config/initializers/mime_types.rb +5 -0
- data/spec/dummy-2-3-12/config/initializers/new_rails_defaults.rb +21 -0
- data/spec/dummy-2-3-12/config/initializers/session_store.rb +15 -0
- data/spec/dummy-2-3-12/config/locales/en.yml +5 -0
- data/spec/dummy-2-3-12/config/routes.rb +43 -0
- data/spec/dummy-2-3-12/db/seeds.rb +7 -0
- data/spec/dummy-2-3-12/public/404.html +30 -0
- data/spec/dummy-2-3-12/public/422.html +30 -0
- data/spec/dummy-2-3-12/public/500.html +30 -0
- data/spec/dummy-2-3-12/public/favicon.ico +0 -0
- data/spec/dummy-2-3-12/public/images/rails.png +0 -0
- data/spec/dummy-2-3-12/public/index.html +275 -0
- data/spec/dummy-2-3-12/public/javascripts/application.js +2 -0
- data/spec/dummy-2-3-12/public/javascripts/controls.js +963 -0
- data/spec/dummy-2-3-12/public/javascripts/dragdrop.js +973 -0
- data/spec/dummy-2-3-12/public/javascripts/effects.js +1128 -0
- data/spec/dummy-2-3-12/public/javascripts/prototype.js +4320 -0
- data/spec/dummy-2-3-12/public/robots.txt +5 -0
- data/spec/dummy-2-3-12/script/about +4 -0
- data/spec/dummy-2-3-12/script/console +3 -0
- data/spec/dummy-2-3-12/script/dbconsole +3 -0
- data/spec/dummy-2-3-12/script/destroy +3 -0
- data/spec/dummy-2-3-12/script/generate +3 -0
- data/spec/dummy-2-3-12/script/performance/benchmarker +3 -0
- data/spec/dummy-2-3-12/script/performance/profiler +3 -0
- data/spec/dummy-2-3-12/script/plugin +3 -0
- data/spec/dummy-2-3-12/script/runner +3 -0
- data/spec/dummy-2-3-12/script/server +3 -0
- data/spec/helper.rb +47 -0
- data/spec/models/ao_crudify_spec.rb +62 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +31 -0
- metadata +193 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.md
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2005-2010 [kame]
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
AoCrudify
|
2
|
+
=======
|
3
|
+
|
4
|
+
A dynamic resource controller for Rails > 2.3 and ActiveObject ORM that keeps your controllers nice and skinny.
|
5
|
+
|
6
|
+
AoCrudify was inspired from Crudify.
|
7
|
+
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
|
12
|
+
It's best to install crudify by adding your Rails project's Gemfile:
|
13
|
+
|
14
|
+
# Gemfile
|
15
|
+
source "http://rubygems.org"
|
16
|
+
gem 'crudify', '>= 0.1.0'
|
17
|
+
|
18
|
+
Now run:
|
19
|
+
|
20
|
+
bundle install
|
21
|
+
|
22
|
+
|
23
|
+
Usage
|
24
|
+
-----
|
25
|
+
|
26
|
+
In its most basic form, crudify is designed to be use like this:
|
27
|
+
|
28
|
+
class JelliesController < ApplicationController
|
29
|
+
crudify :jelly
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
Say you want to customize an action that's being defined by crudify, simply overwrite it!
|
34
|
+
|
35
|
+
class JelliesController < ApplicationController
|
36
|
+
crudify :jelly
|
37
|
+
|
38
|
+
def create
|
39
|
+
@jelly = Jelly.new(params[:jelly])
|
40
|
+
# ... the rest of your custom action
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
|
45
|
+
Ok that seems easy enough, but what if my action is just a tiny bit different? That's where the _hook methods_ come in...
|
46
|
+
|
47
|
+
### Hook Methods
|
48
|
+
|
49
|
+
`before_create`
|
50
|
+
`before_update`
|
51
|
+
`before_destroy`
|
52
|
+
`before_action`
|
53
|
+
|
54
|
+
`successful_create`
|
55
|
+
`successful_update`
|
56
|
+
`successful_destroy`
|
57
|
+
`after_success`
|
58
|
+
|
59
|
+
`failed_create`
|
60
|
+
`failed_update`
|
61
|
+
`failed_destroy`
|
62
|
+
`after_fail`
|
63
|
+
|
64
|
+
You can define hook_methods in your controller just like this:
|
65
|
+
|
66
|
+
def before_create
|
67
|
+
do_something
|
68
|
+
supre
|
69
|
+
end
|
70
|
+
|
71
|
+
Notice: that `before_create` calls a second hook; `before_action`. This is a generic hook that fires before every crud method's call to `save`, `update` or `destroy`. This means it might be helpful for you to call `super` when overwriting this method so that the chain of hooks keeps firing.
|
72
|
+
|
73
|
+
Notice: if hook_methods return false, the action will return. Because in action it call hook_methods just like this:
|
74
|
+
|
75
|
+
def create
|
76
|
+
...
|
77
|
+
return if before_create === false
|
78
|
+
...
|
79
|
+
end
|
80
|
+
|
81
|
+
Here's an example of a `before_create` hook:
|
82
|
+
|
83
|
+
class PostsController < ApplicationController
|
84
|
+
crudify :post
|
85
|
+
|
86
|
+
def before_create
|
87
|
+
@post.author == current_user
|
88
|
+
super
|
89
|
+
end
|
90
|
+
|
91
|
+
end
|
92
|
+
|
93
|
+
### MIT License
|
data/Rakefile
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rake/testtask'
|
2
|
+
require 'spec/rake/spectask'
|
3
|
+
|
4
|
+
desc 'Default: run unit tests.'
|
5
|
+
task :default => :test
|
6
|
+
|
7
|
+
desc 'Test the ao_locked gem.'
|
8
|
+
Spec::Rake::SpecTask.new(:test) do |t|
|
9
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
10
|
+
t.spec_opts = ['-c','-f','nested']
|
11
|
+
end
|
data/ao_crudify.gemspec
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "ao_crudify/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
|
7
|
+
s.name = "ao_crudify"
|
8
|
+
s.version = AoCrudify::VERSION
|
9
|
+
s.platform = Gem::Platform::RUBY
|
10
|
+
s.authors = ["kame"]
|
11
|
+
s.email = ["kamechb@gmail.com"]
|
12
|
+
s.homepage = "http://github.com/kamechb/ao_crudify"
|
13
|
+
|
14
|
+
s.summary = %q{A dynamic resource controller for Rails > 2.3 and ActiveObject ORM that keeps your controllers nice and skinny.}
|
15
|
+
s.description = %q{A dynamic resource controller for Rails > 2.3 and ActiveObject ORM that keeps your controllers nice and skinny.}
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
|
21
|
+
s.add_dependency('rails', '>= 2.3.12')
|
22
|
+
s.add_dependency('activeobject', '>= 0.3.4')
|
23
|
+
s.add_dependency('rspec', '~> 1.3.0')
|
24
|
+
s.add_dependency('rspec-rails', '~> 1.3.2')
|
25
|
+
|
26
|
+
end
|
@@ -0,0 +1,8 @@
|
|
1
|
+
en:
|
2
|
+
ao_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/lib/ao_crudify.rb
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'action_controller'
|
3
|
+
|
4
|
+
module AoCrudify
|
5
|
+
|
6
|
+
if Rails.version > '3'
|
7
|
+
class Engine < Rails::Engine
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
autoload :HookMethods, 'ao_crudify/hook_methods'
|
12
|
+
autoload :ClassMethods, 'ao_crudify/class_methods'
|
13
|
+
autoload :Base, 'ao_crudify/base'
|
14
|
+
|
15
|
+
end
|
16
|
+
|
17
|
+
I18n.load_path << File.expand_path('../../config/locales/en.yml', __FILE__)
|
18
|
+
I18n.load_path << File.expand_path('../../config/locales/zh-cn.yml', __FILE__)
|
19
|
+
|
20
|
+
ActionController::Base.send(:include, AoCrudify::Base)
|
@@ -0,0 +1,43 @@
|
|
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
|
+
# Example (add to your controller):
|
7
|
+
# crudify :foo, { :title_attribute => 'name' }
|
8
|
+
|
9
|
+
|
10
|
+
module AoCrudify
|
11
|
+
module ResponseCode
|
12
|
+
SUCCESS = 1
|
13
|
+
ERROR = 0
|
14
|
+
end
|
15
|
+
|
16
|
+
module Base
|
17
|
+
def self.default_options(model_name)
|
18
|
+
singular_name = model_name.to_s
|
19
|
+
plural_name = singular_name.pluralize
|
20
|
+
class_name = singular_name.camelize
|
21
|
+
{
|
22
|
+
:title_attribute => singular_name,
|
23
|
+
:singular_name => singular_name,
|
24
|
+
:plural_name => plural_name,
|
25
|
+
:class_name => class_name,
|
26
|
+
:paginate => true,
|
27
|
+
:order_by => :created_at,
|
28
|
+
:direction => :numdesc,
|
29
|
+
:conditions => {},
|
30
|
+
:redirect_to_url => "admin_#{plural_name}_path",
|
31
|
+
:log => Rails.env == 'development'
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.append_features(base)
|
36
|
+
super
|
37
|
+
base.send(:include, AoCrudify::HookMethods)
|
38
|
+
base.extend(AoCrudify::ClassMethods)
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
@@ -0,0 +1,137 @@
|
|
1
|
+
#
|
2
|
+
# Disclaimer:
|
3
|
+
#
|
4
|
+
# The majority of this was originally written by
|
5
|
+
# the splendid fellows at Resolve Digital for their
|
6
|
+
# awesome refinerycms project.
|
7
|
+
#
|
8
|
+
|
9
|
+
module AoCrudify
|
10
|
+
|
11
|
+
module ClassMethods
|
12
|
+
|
13
|
+
def crudify(model_name, options = {})
|
14
|
+
|
15
|
+
options = ::AoCrudify::Base.default_options(model_name).merge(options)
|
16
|
+
|
17
|
+
singular_name = options[:singular_name]
|
18
|
+
plural_name = options[:plural_name]
|
19
|
+
class_name = options[:class_name]
|
20
|
+
klass = class_name.constantize
|
21
|
+
|
22
|
+
options[:paginate] = options[:paginate] && klass.respond_to?(:paginate)
|
23
|
+
|
24
|
+
module_eval %(
|
25
|
+
|
26
|
+
# (Just a comment!)
|
27
|
+
|
28
|
+
before_filter :find_#{singular_name},
|
29
|
+
:only => [:update, :destroy, :edit, :show]
|
30
|
+
|
31
|
+
before_filter :set_crud_options
|
32
|
+
|
33
|
+
def set_crud_options
|
34
|
+
@crud_options ||= #{options.inspect}
|
35
|
+
end
|
36
|
+
|
37
|
+
def what
|
38
|
+
@what ||= '#{options[:title_attribute]}'
|
39
|
+
end
|
40
|
+
|
41
|
+
def index
|
42
|
+
end
|
43
|
+
|
44
|
+
def show
|
45
|
+
end
|
46
|
+
|
47
|
+
def new
|
48
|
+
@#{singular_name} = #{class_name}.new
|
49
|
+
end
|
50
|
+
|
51
|
+
|
52
|
+
def create
|
53
|
+
@instance = @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
|
54
|
+
return if before_create === false
|
55
|
+
if @instance.save
|
56
|
+
successful_create
|
57
|
+
else
|
58
|
+
failed_create
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def edit
|
63
|
+
|
64
|
+
end
|
65
|
+
|
66
|
+
def update
|
67
|
+
return if before_update === false
|
68
|
+
if @#{singular_name}.update_attributes(params[:#{singular_name}])
|
69
|
+
successful_update
|
70
|
+
else
|
71
|
+
failed_update
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
def destroy
|
76
|
+
return if before_destroy === false
|
77
|
+
if @#{singular_name}.destroy
|
78
|
+
successful_destroy
|
79
|
+
else
|
80
|
+
failed_destroy
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
# Finds one single result based on the id params.
|
85
|
+
def find_#{singular_name}
|
86
|
+
set_instance(#{class_name}.find(params[:id]))
|
87
|
+
end
|
88
|
+
|
89
|
+
def paginate_all_#{plural_name}
|
90
|
+
set_collection(#{class_name}.paginate paginate_options.merge(conditions))
|
91
|
+
end
|
92
|
+
|
93
|
+
def find_all_#{plural_name}
|
94
|
+
set_collection(#{class_name}.all conditions)
|
95
|
+
end
|
96
|
+
|
97
|
+
def paginate_options
|
98
|
+
@paginate_options ||= {
|
99
|
+
:page => params[:page],
|
100
|
+
:per_page => params[:per_page],
|
101
|
+
:order_by => :#{options[:order_by]},
|
102
|
+
:direction => :#{options[:direction]}
|
103
|
+
}
|
104
|
+
end
|
105
|
+
|
106
|
+
def conditions
|
107
|
+
@conditions ||= @crud_options[:conditions]
|
108
|
+
end
|
109
|
+
|
110
|
+
def set_instance(record)
|
111
|
+
@instance = @#{singular_name} = record
|
112
|
+
end
|
113
|
+
|
114
|
+
def set_collection(records)
|
115
|
+
@collection = @#{plural_name} = records
|
116
|
+
end
|
117
|
+
|
118
|
+
)
|
119
|
+
|
120
|
+
|
121
|
+
if options[:paginate]
|
122
|
+
module_eval %(
|
123
|
+
def index
|
124
|
+
paginate_all_#{plural_name}
|
125
|
+
end
|
126
|
+
)
|
127
|
+
else
|
128
|
+
module_eval %(
|
129
|
+
def index
|
130
|
+
find_all_#{plural_name}
|
131
|
+
end
|
132
|
+
)
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
end
|
@@ -0,0 +1,117 @@
|
|
1
|
+
module AoCrudify
|
2
|
+
|
3
|
+
module HookMethods
|
4
|
+
|
5
|
+
private
|
6
|
+
|
7
|
+
def before_create
|
8
|
+
# just a hook!
|
9
|
+
puts "> Crud::before_create" if @crud_options[:log]
|
10
|
+
before_action
|
11
|
+
end
|
12
|
+
|
13
|
+
def before_update
|
14
|
+
# just a hook!
|
15
|
+
puts "> Crud::before_update" if @crud_options[:log]
|
16
|
+
before_action
|
17
|
+
end
|
18
|
+
|
19
|
+
def before_destroy
|
20
|
+
# just a hook!
|
21
|
+
puts "> Crud::before_destroy" if @crud_options[:log]
|
22
|
+
before_action
|
23
|
+
end
|
24
|
+
|
25
|
+
def before_action
|
26
|
+
# just a hook!
|
27
|
+
puts "> Crud::before_action" if @crud_options[:log]
|
28
|
+
true
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
def successful_create
|
33
|
+
puts "> Crud::successful_create" if @crud_options[:log]
|
34
|
+
|
35
|
+
flash[:notice] = t('ao_crudify.created', :what => what)
|
36
|
+
|
37
|
+
after_success
|
38
|
+
end
|
39
|
+
|
40
|
+
def successful_update
|
41
|
+
puts "> Crud::successful_update" if @crud_options[:log]
|
42
|
+
|
43
|
+
flash[:notice] = t('ao_crudify.updated', :what => what)
|
44
|
+
|
45
|
+
after_success
|
46
|
+
end
|
47
|
+
|
48
|
+
def successful_destroy
|
49
|
+
puts "> Crud::successful_destroy" if @crud_options[:log]
|
50
|
+
flash[:notice] = t('ao_crudify.destroyed', :what => what)
|
51
|
+
|
52
|
+
after_success
|
53
|
+
end
|
54
|
+
|
55
|
+
def after_success
|
56
|
+
puts "> Crud::after_success" if @crud_options[:log]
|
57
|
+
respond_to do |f|
|
58
|
+
f.html {
|
59
|
+
if request.xhr?
|
60
|
+
render @ajax_render_options and return if @ajax_render_options
|
61
|
+
render :layout => false
|
62
|
+
else
|
63
|
+
redirect_to params[:redirect] and return if params[:redirect] && whitelist_redirect(params[:redirect])
|
64
|
+
redirect_to @redirect_to_url and return if @redirect_to_url
|
65
|
+
redirect_to @crud_options[:redirect_to_url] and return if @crud_options[:redirect_to_url]
|
66
|
+
render
|
67
|
+
end
|
68
|
+
}
|
69
|
+
f.json { render :json => {:code => ResponseCode::SUCCESS} }
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
|
76
|
+
def failed_create
|
77
|
+
puts "> Crud::failed_create" if @crud_options[:log]
|
78
|
+
flash[:error] = t('ao_crudify.failed_create', :what => what)
|
79
|
+
after_fail
|
80
|
+
end
|
81
|
+
|
82
|
+
def failed_update
|
83
|
+
puts "> Crud::failed_update" if @crud_options[:log]
|
84
|
+
flash[:error] = t('ao_crudify.failed_update', :what => what)
|
85
|
+
after_fail
|
86
|
+
end
|
87
|
+
|
88
|
+
def failed_destroy
|
89
|
+
puts "> Crud::failed_destroy" if @crud_options[:log]
|
90
|
+
flash[:error] = t('ao_crudify.failed_destroy', :what => what)
|
91
|
+
after_fail
|
92
|
+
end
|
93
|
+
|
94
|
+
def after_fail
|
95
|
+
puts "> Crud::after_fail" if @crud_options[:log]
|
96
|
+
respond_to do |f|
|
97
|
+
f.html {
|
98
|
+
if request.xhr?
|
99
|
+
render :text => 'error'
|
100
|
+
else
|
101
|
+
render :text => 'error' and return if request.delete?
|
102
|
+
render :action => :new and return if request.post?
|
103
|
+
render :action => :edit
|
104
|
+
end
|
105
|
+
}
|
106
|
+
f.json { render :json => {:code => ResponseCode::ERROR} }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
# 对redirect url进行白名单过滤,防止跳转到attack的网站, default true.
|
111
|
+
def whitelist_redirect(url)
|
112
|
+
true
|
113
|
+
end
|
114
|
+
|
115
|
+
end
|
116
|
+
|
117
|
+
end
|