active_crudify 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/Gemfile +13 -0
- data/Gemfile.lock +123 -0
- data/README.md +83 -0
- data/active_crudify.gemspec +22 -0
- data/config/locales/en.yml +9 -0
- data/config/locales/zh-CN.yml +9 -0
- data/lib/active_crudify.rb +39 -0
- data/lib/active_crudify/class_methods.rb +159 -0
- data/lib/active_crudify/hook_methods.rb +22 -0
- data/lib/active_crudify/responder.rb +6 -0
- data/spec/controllers/admin/posts_controller_spec.rb +33 -0
- data/spec/controllers/posts_controller_spec.rb +126 -0
- data/spec/dummy/.gitignore +15 -0
- data/spec/dummy/Gemfile +38 -0
- data/spec/dummy/Gemfile.lock +111 -0
- data/spec/dummy/README.rdoc +261 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/assets/images/rails.png +0 -0
- data/spec/dummy/app/assets/javascripts/application.js +15 -0
- data/spec/dummy/app/assets/stylesheets/application.css +13 -0
- data/spec/dummy/app/controllers/admin/posts_controller.rb +3 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/controllers/posts_controller.rb +4 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/mailers/.gitkeep +0 -0
- data/spec/dummy/app/models/.gitkeep +0 -0
- data/spec/dummy/app/models/post.rb +3 -0
- data/spec/dummy/app/views/admin/posts/index.html.erb +0 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/app/views/posts/edit.html.erb +0 -0
- data/spec/dummy/app/views/posts/index.html.erb +0 -0
- data/spec/dummy/app/views/posts/new.html.erb +0 -0
- data/spec/dummy/app/views/posts/show.html.erb +0 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/config/application.rb +59 -0
- data/spec/dummy/config/boot.rb +6 -0
- data/spec/dummy/config/database.yml +25 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +37 -0
- data/spec/dummy/config/environments/production.rb +67 -0
- data/spec/dummy/config/environments/test.rb +37 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +15 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +62 -0
- data/spec/dummy/db/migrate/20120521063240_create_posts.rb +10 -0
- data/spec/dummy/db/schema.rb +23 -0
- data/spec/dummy/db/seeds.rb +7 -0
- data/spec/dummy/lib/assets/.gitkeep +0 -0
- data/spec/dummy/lib/tasks/.gitkeep +0 -0
- data/spec/dummy/log/.gitkeep +0 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +25 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/index.html +241 -0
- data/spec/dummy/public/robots.txt +5 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/dummy/test/fixtures/posts.yml +9 -0
- data/spec/dummy/test/unit/post_test.rb +7 -0
- data/spec/spec.opts +5 -0
- data/spec/spec_helper.rb +44 -0
- metadata +162 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,123 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
active_crudify (0.1.0)
|
5
|
+
actionpack (>= 3.1)
|
6
|
+
kaminari
|
7
|
+
responders
|
8
|
+
|
9
|
+
GEM
|
10
|
+
remote: http://rubygems.org/
|
11
|
+
specs:
|
12
|
+
actionmailer (3.2.3)
|
13
|
+
actionpack (= 3.2.3)
|
14
|
+
mail (~> 2.4.4)
|
15
|
+
actionpack (3.2.3)
|
16
|
+
activemodel (= 3.2.3)
|
17
|
+
activesupport (= 3.2.3)
|
18
|
+
builder (~> 3.0.0)
|
19
|
+
erubis (~> 2.7.0)
|
20
|
+
journey (~> 1.0.1)
|
21
|
+
rack (~> 1.4.0)
|
22
|
+
rack-cache (~> 1.2)
|
23
|
+
rack-test (~> 0.6.1)
|
24
|
+
sprockets (~> 2.1.2)
|
25
|
+
activemodel (3.2.3)
|
26
|
+
activesupport (= 3.2.3)
|
27
|
+
builder (~> 3.0.0)
|
28
|
+
activerecord (3.2.3)
|
29
|
+
activemodel (= 3.2.3)
|
30
|
+
activesupport (= 3.2.3)
|
31
|
+
arel (~> 3.0.2)
|
32
|
+
tzinfo (~> 0.3.29)
|
33
|
+
activeresource (3.2.3)
|
34
|
+
activemodel (= 3.2.3)
|
35
|
+
activesupport (= 3.2.3)
|
36
|
+
activesupport (3.2.3)
|
37
|
+
i18n (~> 0.6)
|
38
|
+
multi_json (~> 1.0)
|
39
|
+
arel (3.0.2)
|
40
|
+
builder (3.0.0)
|
41
|
+
database_cleaner (0.7.2)
|
42
|
+
diff-lcs (1.1.3)
|
43
|
+
erubis (2.7.0)
|
44
|
+
factory_girl (3.0.0)
|
45
|
+
activesupport (>= 3.0.0)
|
46
|
+
hike (1.2.1)
|
47
|
+
i18n (0.6.0)
|
48
|
+
journey (1.0.3)
|
49
|
+
json (1.7.1)
|
50
|
+
kaminari (0.13.0)
|
51
|
+
actionpack (>= 3.0.0)
|
52
|
+
activesupport (>= 3.0.0)
|
53
|
+
railties (>= 3.0.0)
|
54
|
+
mail (2.4.4)
|
55
|
+
i18n (>= 0.4.0)
|
56
|
+
mime-types (~> 1.16)
|
57
|
+
treetop (~> 1.4.8)
|
58
|
+
mime-types (1.18)
|
59
|
+
multi_json (1.3.4)
|
60
|
+
polyglot (0.3.3)
|
61
|
+
rack (1.4.1)
|
62
|
+
rack-cache (1.2)
|
63
|
+
rack (>= 0.4)
|
64
|
+
rack-ssl (1.3.2)
|
65
|
+
rack
|
66
|
+
rack-test (0.6.1)
|
67
|
+
rack (>= 1.0)
|
68
|
+
rails (3.2.3)
|
69
|
+
actionmailer (= 3.2.3)
|
70
|
+
actionpack (= 3.2.3)
|
71
|
+
activerecord (= 3.2.3)
|
72
|
+
activeresource (= 3.2.3)
|
73
|
+
activesupport (= 3.2.3)
|
74
|
+
bundler (~> 1.0)
|
75
|
+
railties (= 3.2.3)
|
76
|
+
railties (3.2.3)
|
77
|
+
actionpack (= 3.2.3)
|
78
|
+
activesupport (= 3.2.3)
|
79
|
+
rack-ssl (~> 1.3.2)
|
80
|
+
rake (>= 0.8.7)
|
81
|
+
rdoc (~> 3.4)
|
82
|
+
thor (~> 0.14.6)
|
83
|
+
rake (0.9.2.2)
|
84
|
+
rdoc (3.12)
|
85
|
+
json (~> 1.4)
|
86
|
+
responders (0.9.1)
|
87
|
+
railties (~> 3.1)
|
88
|
+
rspec (2.9.0)
|
89
|
+
rspec-core (~> 2.9.0)
|
90
|
+
rspec-expectations (~> 2.9.0)
|
91
|
+
rspec-mocks (~> 2.9.0)
|
92
|
+
rspec-core (2.9.0)
|
93
|
+
rspec-expectations (2.9.0)
|
94
|
+
diff-lcs (~> 1.1.3)
|
95
|
+
rspec-mocks (2.9.0)
|
96
|
+
rspec-rails (2.9.0)
|
97
|
+
actionpack (>= 3.0)
|
98
|
+
activesupport (>= 3.0)
|
99
|
+
railties (>= 3.0)
|
100
|
+
rspec (~> 2.9.0)
|
101
|
+
sprockets (2.1.3)
|
102
|
+
hike (~> 1.2)
|
103
|
+
rack (~> 1.0)
|
104
|
+
tilt (~> 1.1, != 1.3.0)
|
105
|
+
sqlite3 (1.3.6)
|
106
|
+
thor (0.14.6)
|
107
|
+
tilt (1.3.3)
|
108
|
+
treetop (1.4.10)
|
109
|
+
polyglot
|
110
|
+
polyglot (>= 0.3.1)
|
111
|
+
tzinfo (0.3.33)
|
112
|
+
|
113
|
+
PLATFORMS
|
114
|
+
ruby
|
115
|
+
|
116
|
+
DEPENDENCIES
|
117
|
+
active_crudify!
|
118
|
+
database_cleaner
|
119
|
+
factory_girl
|
120
|
+
rails
|
121
|
+
rspec
|
122
|
+
rspec-rails
|
123
|
+
sqlite3
|
data/README.md
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
ActiveCrudify
|
2
|
+
=======
|
3
|
+
|
4
|
+
A dynamic resource controller for Rails > 3.0 that keeps your controllers nice and skinny.
|
5
|
+
|
6
|
+
ActiveCrudify was inspired from Crudify.
|
7
|
+
|
8
|
+
|
9
|
+
Installation
|
10
|
+
------------
|
11
|
+
|
12
|
+
It's best to install ActiveCrudify by adding your Rails project's Gemfile:
|
13
|
+
|
14
|
+
# Gemfile
|
15
|
+
source "http://rubygems.org"
|
16
|
+
gem 'active_crudify', '>= 0.1.0'
|
17
|
+
|
18
|
+
Now run:
|
19
|
+
|
20
|
+
bundle install
|
21
|
+
|
22
|
+
|
23
|
+
Usage
|
24
|
+
-----
|
25
|
+
|
26
|
+
In its most basic form, active_crudify is designed to be used like this:
|
27
|
+
|
28
|
+
class PostsController < ApplicationController
|
29
|
+
crudify :post
|
30
|
+
end
|
31
|
+
|
32
|
+
|
33
|
+
Say you want to customize an action that's being defined by crudify, simply overwrite it!
|
34
|
+
|
35
|
+
class PostsController < ApplicationController
|
36
|
+
crudify :post
|
37
|
+
|
38
|
+
def create
|
39
|
+
@post = Post.new(params[:post])
|
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
|
+
You can define hook_methods in your controller just like this:
|
55
|
+
|
56
|
+
def before_create
|
57
|
+
do_something
|
58
|
+
supre
|
59
|
+
end
|
60
|
+
|
61
|
+
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.
|
62
|
+
|
63
|
+
Notice: if hook_methods return false, the action will return. Because in action it call hook_methods just like this:
|
64
|
+
|
65
|
+
def create
|
66
|
+
...
|
67
|
+
return if before_create === false
|
68
|
+
...
|
69
|
+
end
|
70
|
+
|
71
|
+
Here's an example of a `before_create` hook:
|
72
|
+
|
73
|
+
class PostsController < ApplicationController
|
74
|
+
crudify :post
|
75
|
+
|
76
|
+
def before_create
|
77
|
+
@post.author == current_user
|
78
|
+
super
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
|
83
|
+
### MIT License
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
|
5
|
+
s.name = "active_crudify"
|
6
|
+
s.version = "0.1.1"
|
7
|
+
s.platform = Gem::Platform::RUBY
|
8
|
+
s.authors = ["kame"]
|
9
|
+
s.email = ["kamechb@gmail.com"]
|
10
|
+
s.homepage = "http://github.com/kamechb/active_crudify"
|
11
|
+
|
12
|
+
s.summary = %q{DRY controller actions for Rails > 3.0 and keeps your controllers nice and skinny.}
|
13
|
+
s.description = %q{DRY controller actions for Rails > 3.0 and keeps your controllers nice and skinny.}
|
14
|
+
|
15
|
+
s.files = `git ls-files`.split("\n")
|
16
|
+
s.test_files = `git ls-files -- {spec}/*`.split("\n")
|
17
|
+
s.require_paths = ["lib"]
|
18
|
+
|
19
|
+
s.add_dependency('actionpack', '>= 3.1')
|
20
|
+
s.add_dependency('kaminari')
|
21
|
+
s.add_dependency('responders')
|
22
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
require 'i18n'
|
3
|
+
require 'responders'
|
4
|
+
require 'action_controller'
|
5
|
+
require 'active_crudify/class_methods'
|
6
|
+
require 'active_crudify/hook_methods'
|
7
|
+
require 'active_crudify/responder'
|
8
|
+
|
9
|
+
module ActiveCrudify
|
10
|
+
class Engine < Rails::Engine
|
11
|
+
end
|
12
|
+
|
13
|
+
extend ActiveSupport::Concern
|
14
|
+
|
15
|
+
included do
|
16
|
+
include ActiveCrudify::HookMethods
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.default_options(model_name)
|
20
|
+
singular_name = model_name.to_s
|
21
|
+
plural_name = singular_name.pluralize
|
22
|
+
class_name = singular_name.camelize
|
23
|
+
{
|
24
|
+
:singular_name => singular_name,
|
25
|
+
:plural_name => plural_name,
|
26
|
+
:class_name => class_name,
|
27
|
+
:paginate => true,
|
28
|
+
:order_by => "created_at DESC",
|
29
|
+
:conditions => {},
|
30
|
+
:namespace => nil,
|
31
|
+
:log => Rails.env == 'development'
|
32
|
+
}
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
ActionController::Base.send(:include, ActiveCrudify)
|
38
|
+
|
39
|
+
|
@@ -0,0 +1,159 @@
|
|
1
|
+
module ActiveCrudify
|
2
|
+
module ClassMethods
|
3
|
+
# === Parameters
|
4
|
+
# * model_name
|
5
|
+
# Specify the model name like :post, :blog
|
6
|
+
# * options
|
7
|
+
#
|
8
|
+
# === Options
|
9
|
+
# [:class_name]
|
10
|
+
# Specify the class name for model.
|
11
|
+
# [:plural_name]
|
12
|
+
# [:order_by]
|
13
|
+
# [:conditions]
|
14
|
+
# Specify the condition which will be used in where clauses.
|
15
|
+
# [:namespace]
|
16
|
+
# Specify the controller in which namespace, like admin.
|
17
|
+
# [:only]
|
18
|
+
# Actions will be defined for controller.
|
19
|
+
# [:except]
|
20
|
+
# Define the actions exclude this option specified.
|
21
|
+
def crudify(model_name, options = {})
|
22
|
+
self.responder = ActiveCrudify::Responder
|
23
|
+
respond_to :html, :json, :xml
|
24
|
+
|
25
|
+
options = ActiveCrudify.default_options(model_name).merge(options)
|
26
|
+
|
27
|
+
allowed_actions = [:index, :new, :create, :show, :edit, :update, :destroy]
|
28
|
+
if options[:only]
|
29
|
+
allowed_actions = options[:only]
|
30
|
+
elsif options[:except]
|
31
|
+
allowed_actions = allowed_actions - options[:except]
|
32
|
+
end
|
33
|
+
|
34
|
+
singular_name = options[:singular_name]
|
35
|
+
plural_name = options[:plural_name]
|
36
|
+
class_name = options[:class_name]
|
37
|
+
klass = class_name.constantize
|
38
|
+
namespace = options[:namespace]
|
39
|
+
responded = namespace ? [namespace] : []
|
40
|
+
|
41
|
+
|
42
|
+
options[:paginate] = options[:paginate] && klass.respond_to?(:page)
|
43
|
+
|
44
|
+
module_eval %(
|
45
|
+
protected
|
46
|
+
|
47
|
+
def set_crud_options
|
48
|
+
@crud_options ||= #{options.inspect}
|
49
|
+
end
|
50
|
+
|
51
|
+
def what
|
52
|
+
@what ||= '#{options[:title]}'
|
53
|
+
end
|
54
|
+
|
55
|
+
def find_#{singular_name}
|
56
|
+
set_instance(#{class_name}.find(params[:id]))
|
57
|
+
end
|
58
|
+
|
59
|
+
def paginate_all_#{plural_name}
|
60
|
+
set_collection(#{class_name}.page(params[:page]).order(@crud_options[:order_by]).where(conditions))
|
61
|
+
end
|
62
|
+
|
63
|
+
def find_all_#{plural_name}
|
64
|
+
set_collection(#{class_name}.order(@crud_options[:order_by]).where(conditions))
|
65
|
+
end
|
66
|
+
|
67
|
+
def conditions
|
68
|
+
@conditions ||= @crud_options[:conditions]
|
69
|
+
end
|
70
|
+
|
71
|
+
def set_instance(record)
|
72
|
+
@instance = @#{singular_name} = record
|
73
|
+
end
|
74
|
+
|
75
|
+
def set_collection(records)
|
76
|
+
@collection = @#{plural_name} = records
|
77
|
+
end
|
78
|
+
)
|
79
|
+
|
80
|
+
module_eval %(
|
81
|
+
|
82
|
+
before_filter :find_#{singular_name},
|
83
|
+
:only => [:update, :destroy, :edit, :show]
|
84
|
+
|
85
|
+
before_filter :set_crud_options
|
86
|
+
|
87
|
+
|
88
|
+
|
89
|
+
if #{allowed_actions.include?(:new)}
|
90
|
+
def new
|
91
|
+
@#{singular_name} = #{class_name}.new
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
if #{allowed_actions.include?(:show)}
|
96
|
+
def show
|
97
|
+
respond_with(@#{singular_name})
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
|
102
|
+
if #{allowed_actions.include?(:create)}
|
103
|
+
def create
|
104
|
+
@instance = @#{singular_name} = #{class_name}.new(params[:#{singular_name}])
|
105
|
+
return if before_create === false
|
106
|
+
@instance.save
|
107
|
+
respond_with(#{namespace.inspect}, @#{singular_name})
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
if #{allowed_actions.include?(:edit)}
|
112
|
+
def edit
|
113
|
+
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
if #{allowed_actions.include?(:update)}
|
118
|
+
def update
|
119
|
+
return if before_update === false
|
120
|
+
@#{singular_name}.update_attributes(params[:#{singular_name}])
|
121
|
+
respond_with(#{namespace.inspect}, @#{singular_name})
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
if #{allowed_actions.include?(:destroy)}
|
126
|
+
def destroy
|
127
|
+
return if before_destroy === false
|
128
|
+
@#{singular_name}.destroy
|
129
|
+
respond_to do |f|
|
130
|
+
f.html { redirect_to([#{namespace.inspect}, :#{plural_name}]) }
|
131
|
+
f.any(:xml, :json) {head :ok}
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
)
|
136
|
+
|
137
|
+
|
138
|
+
if allowed_actions.include?(:index)
|
139
|
+
if options[:paginate]
|
140
|
+
module_eval %(
|
141
|
+
def index
|
142
|
+
paginate_all_#{plural_name}
|
143
|
+
respond_with(#{responded} << @#{plural_name})
|
144
|
+
end
|
145
|
+
)
|
146
|
+
else
|
147
|
+
module_eval %(
|
148
|
+
def index
|
149
|
+
find_all_#{plural_name}
|
150
|
+
respond_with(#{responded} << @#{plural_name})
|
151
|
+
end
|
152
|
+
)
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
159
|
+
end
|