crudcontroller 0.1.2
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/README.rdoc +0 -0
- data/Rakefile +0 -0
- data/crudcontroller.gemspec +12 -0
- data/lib/crudcontroller.rb +168 -0
- data/lib/crudcontroller/version.rb +9 -0
- metadata +71 -0
data/README.rdoc
ADDED
File without changes
|
data/Rakefile
ADDED
File without changes
|
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = "crudcontroller"
|
3
|
+
s.version = "0.1.2"
|
4
|
+
s.rubyforge_project = "rubygems"
|
5
|
+
s.description = "A simple gem that generate crud methods for controllers"
|
6
|
+
s.summary = "Crud Methods for controllers"
|
7
|
+
s.author = "Christiano Milfont - cmilfont"
|
8
|
+
s.email = "cmilfont@gmail.com"
|
9
|
+
s.homepage = "http://www.milfont.org"
|
10
|
+
s.files = Dir["{lib/**/*.rb,README.rdoc,Rakefile,*.gemspec}"]
|
11
|
+
end
|
12
|
+
|
@@ -0,0 +1,168 @@
|
|
1
|
+
module CrudController
|
2
|
+
|
3
|
+
def self.included( base )
|
4
|
+
base.extend( CrudController::ClassMethods )
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
|
9
|
+
def generate_for(model)
|
10
|
+
model = model.to_s
|
11
|
+
klass_name = model.camelize
|
12
|
+
plural = model.pluralize
|
13
|
+
|
14
|
+
class_eval %Q!
|
15
|
+
|
16
|
+
before_filter :load_page, :load_conditions, :only => :index
|
17
|
+
before_filter :load_#{model}, :only => [ :edit, :new, :create, :update, :destroy ]
|
18
|
+
|
19
|
+
def index
|
20
|
+
load_records
|
21
|
+
respond_to do |format|
|
22
|
+
format.html
|
23
|
+
format.json { render :json => { :results => @#{plural},
|
24
|
+
:total => @#{plural}.total_entries
|
25
|
+
}.to_json( to_json_options )
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def show
|
31
|
+
@#{model} = #{klass_name}.find(params[:id])
|
32
|
+
end
|
33
|
+
|
34
|
+
def edit
|
35
|
+
on_edit( @#{model} )
|
36
|
+
render :edit
|
37
|
+
end
|
38
|
+
|
39
|
+
alias :new :edit
|
40
|
+
|
41
|
+
def create
|
42
|
+
create_or_update
|
43
|
+
end
|
44
|
+
|
45
|
+
def update
|
46
|
+
create_or_update
|
47
|
+
end
|
48
|
+
|
49
|
+
def destroy
|
50
|
+
@#{model}.destroy
|
51
|
+
respond_to do |format|
|
52
|
+
format.json { render :json => {
|
53
|
+
:success => 'true'
|
54
|
+
}
|
55
|
+
}
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
protected
|
60
|
+
|
61
|
+
def load_records
|
62
|
+
@#{plural} = paginate( #{klass_name}, (paginate_options.merge({:conditions => @conditions})) )
|
63
|
+
end
|
64
|
+
|
65
|
+
def paginate_options
|
66
|
+
{}
|
67
|
+
end
|
68
|
+
|
69
|
+
def to_json_options
|
70
|
+
{}
|
71
|
+
end
|
72
|
+
|
73
|
+
def on_edit(record)
|
74
|
+
end
|
75
|
+
|
76
|
+
def on_create_or_update(record)
|
77
|
+
end
|
78
|
+
|
79
|
+
def load_#{model}
|
80
|
+
@#{model} = params[:id].blank? ? #{klass_name}.new : #{klass_name}.find(params[:id])
|
81
|
+
end
|
82
|
+
|
83
|
+
def paginate( #{model}, options = {} )
|
84
|
+
#{model}.paginate( { :page => @page, :per_page => @per_page }.merge(options) )
|
85
|
+
end
|
86
|
+
|
87
|
+
def load_page
|
88
|
+
@page = params[:page] || '1'
|
89
|
+
@per_page = (params[:limit] || '100').to_i
|
90
|
+
true
|
91
|
+
end
|
92
|
+
|
93
|
+
def create_or_update
|
94
|
+
on_create_or_update( @#{model} )
|
95
|
+
status = @#{model}.new_record? ? :created : :ok
|
96
|
+
if @#{model}.update_attributes(params[:#{model}])
|
97
|
+
respond_to do |format|
|
98
|
+
format.html { redirect_to(@#{model}) }
|
99
|
+
format.json { render :json => {
|
100
|
+
:success => 'true',
|
101
|
+
:id => @#{model}.id,
|
102
|
+
:record => @#{model}
|
103
|
+
},
|
104
|
+
:status => status,
|
105
|
+
:location => @#{model}
|
106
|
+
}
|
107
|
+
end
|
108
|
+
else
|
109
|
+
respond_to do |format|
|
110
|
+
@errors = { :attributes => {}, :base => @#{model}.errors.on_base}
|
111
|
+
|
112
|
+
@#{model}.errors.each do |attr, msg|
|
113
|
+
@errors[:attributes]["#{model}_\#{attr}"] ||= []
|
114
|
+
@errors[:attributes]["#{model}_\#{attr}_id"] ||= []
|
115
|
+
@errors[:attributes]["#{model}_\#{attr}"] << msg
|
116
|
+
@errors[:attributes]["#{model}_\#{attr}_id"] << msg
|
117
|
+
end
|
118
|
+
|
119
|
+
format.html { render :action => "new" }
|
120
|
+
format.json { render :json => { :success => 'false',
|
121
|
+
:errors => @errors
|
122
|
+
},
|
123
|
+
:status => :unprocessable_entity,
|
124
|
+
:location => @#{model}
|
125
|
+
}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
end
|
129
|
+
|
130
|
+
def load_conditions
|
131
|
+
fieldsForSearch = eval(params[:fields]) if params[:fields]
|
132
|
+
|
133
|
+
|
134
|
+
query = params[:query] if params[:query]
|
135
|
+
|
136
|
+
aditional_filter = params[:aditionalFilter] ? params[:aditionalFilter] : ''
|
137
|
+
|
138
|
+
if (query == "")
|
139
|
+
query = false
|
140
|
+
end
|
141
|
+
|
142
|
+
conditions_array = []
|
143
|
+
|
144
|
+
if (fieldsForSearch && query)
|
145
|
+
fieldsForSearch.each do |field|
|
146
|
+
if (#{klass_name}.columns_hash[field].type == :string)
|
147
|
+
conditions_array << \"UPPER(\#{field}) LIKE UPPER('%\#{query}%'\)"
|
148
|
+
else
|
149
|
+
if (query.to_i \!= 0)
|
150
|
+
conditions_array << \"\#{field} = \#{query}\"
|
151
|
+
end
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
conditions_array << \"\#{aditional_filter}\" unless aditional_filter.blank?
|
157
|
+
|
158
|
+
@conditions = conditions_array.join(' OR ')
|
159
|
+
end
|
160
|
+
|
161
|
+
!
|
162
|
+
|
163
|
+
end
|
164
|
+
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
|
metadata
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: crudcontroller
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 31
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 1
|
9
|
+
- 2
|
10
|
+
version: 0.1.2
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Christiano Milfont - cmilfont
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-08-10 00:00:00 -03:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: A simple gem that generate crud methods for controllers
|
23
|
+
email: cmilfont@gmail.com
|
24
|
+
executables: []
|
25
|
+
|
26
|
+
extensions: []
|
27
|
+
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
|
+
files:
|
31
|
+
- lib/crudcontroller/version.rb
|
32
|
+
- lib/crudcontroller.rb
|
33
|
+
- README.rdoc
|
34
|
+
- Rakefile
|
35
|
+
- crudcontroller.gemspec
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://www.milfont.org
|
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
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
hash: 3
|
51
|
+
segments:
|
52
|
+
- 0
|
53
|
+
version: "0"
|
54
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
55
|
+
none: false
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
hash: 3
|
60
|
+
segments:
|
61
|
+
- 0
|
62
|
+
version: "0"
|
63
|
+
requirements: []
|
64
|
+
|
65
|
+
rubyforge_project: rubygems
|
66
|
+
rubygems_version: 1.3.7
|
67
|
+
signing_key:
|
68
|
+
specification_version: 3
|
69
|
+
summary: Crud Methods for controllers
|
70
|
+
test_files: []
|
71
|
+
|