humdrum-rails 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
Files changed (88) hide show
  1. data/lib/humdrum/version.rb +1 -1
  2. data/lib/rails/generators/humdrum/api/api_generator.rb +339 -0
  3. data/lib/rails/generators/humdrum/api/templates/controllers/resource_controller.rb +207 -0
  4. data/lib/rails/generators/humdrum/api/templates/javascripts/validations/tbr_untitled.js +114 -0
  5. data/lib/rails/generators/humdrum/api/templates/javascripts/validations/validator.js +85 -0
  6. data/lib/rails/generators/humdrum/api/templates/migrations/create_resources.rb +14 -0
  7. data/lib/rails/generators/humdrum/api/templates/models/resource.rb +32 -0
  8. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_edit.html.erb +7 -0
  9. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_field.html.erb +41 -0
  10. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_filters.html.erb +9 -0
  11. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_form.html.erb +81 -0
  12. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_index.html.erb +22 -0
  13. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_item.html.erb +21 -0
  14. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_nav_filters.html.erb +9 -0
  15. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_new.html.erb +9 -0
  16. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_show.html.erb +48 -0
  17. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/_summary.html.erb +10 -0
  18. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/create.js.erb +15 -0
  19. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/destroy.js.erb +14 -0
  20. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/edit.js.erb +2 -0
  21. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/index.html.erb +33 -0
  22. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/index.js.erb +15 -0
  23. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/new.js.erb +2 -0
  24. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/show.js.erb +2 -0
  25. data/lib/rails/generators/humdrum/api/templates/views/bootstrap/resource/update.js.erb +14 -0
  26. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_edit.html.erb +7 -0
  27. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_field.html.erb +41 -0
  28. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_filters.html.erb +18 -0
  29. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_form.html copy.erb +71 -0
  30. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_form.html.erb +49 -0
  31. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_index.html.erb +22 -0
  32. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_item.html.erb +21 -0
  33. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_nav_filters.html.erb +9 -0
  34. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_new.html.erb +9 -0
  35. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_show.html.erb +58 -0
  36. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/_summary.html.erb +10 -0
  37. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/create.js.erb +22 -0
  38. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/destroy.js.erb +14 -0
  39. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/edit.js.erb +3 -0
  40. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/index.html.erb +37 -0
  41. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/index.js.erb +15 -0
  42. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/new.js.erb +3 -0
  43. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/show.js.erb +2 -0
  44. data/lib/rails/generators/humdrum/api/templates/views/gumby/resource/update.js.erb +21 -0
  45. data/lib/rails/generators/humdrum/layout/layout_generator.rb +42 -29
  46. data/lib/rails/generators/humdrum/layout/templates/controllers/application_controller.rb +2 -9
  47. data/lib/rails/generators/humdrum/layout/templates/helpers/flash_helper.rb +13 -0
  48. data/lib/rails/generators/humdrum/layout/templates/helpers/params_parser_helper.rb +31 -0
  49. data/lib/rails/generators/humdrum/layout/templates/javascripts/application.js +5 -0
  50. data/lib/rails/generators/humdrum/layout/templates/javascripts/validations/main.js +15 -0
  51. data/lib/rails/generators/humdrum/layout/templates/stylesheets/application.css +2 -0
  52. data/lib/rails/generators/humdrum/layout/templates/stylesheets/bootstrap-responsive.css +1109 -0
  53. data/lib/rails/generators/humdrum/layout/templates/stylesheets/gumby-pagination.css +56 -0
  54. data/lib/rails/generators/humdrum/layout/templates/stylesheets/overrides-bootstrap.css +1 -1
  55. data/lib/rails/generators/humdrum/layout/templates/stylesheets/overrides-gumby.css +8 -0
  56. data/lib/rails/generators/humdrum/layout/templates/views/bootstrap/layouts/admin.html.erb +4 -4
  57. data/lib/rails/generators/humdrum/layout/templates/views/bootstrap/layouts/admin/_header.html.erb +1 -1
  58. data/lib/rails/generators/humdrum/layout/templates/views/bootstrap/layouts/public.html.erb +2 -2
  59. data/lib/rails/generators/humdrum/layout/templates/views/bootstrap/layouts/public/_header.html.erb +1 -1
  60. data/lib/rails/generators/humdrum/layout/templates/views/bootstrap/layouts/user.html.erb +2 -2
  61. data/lib/rails/generators/humdrum/layout/templates/views/bootstrap/layouts/user/_header.html.erb +1 -1
  62. data/lib/rails/generators/humdrum/resource/resource_generator.rb +60 -38
  63. data/lib/rails/generators/humdrum/resource/templates/controllers/resource_controller.rb +10 -2
  64. data/lib/rails/generators/humdrum/resource/templates/javascripts/validations/validator.js +78 -25
  65. data/lib/rails/generators/humdrum/resource/templates/migrations/create_resources.rb +1 -1
  66. data/lib/rails/generators/humdrum/resource/templates/models/resource.rb +28 -1
  67. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_edit.html.erb +1 -1
  68. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_field.html.erb +41 -0
  69. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_form.html.erb +59 -22
  70. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_item.html.erb +10 -10
  71. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_new.html.erb +1 -1
  72. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_show.html.erb +13 -3
  73. data/lib/rails/generators/humdrum/resource/templates/views/bootstrap/resource/_summary.html.erb +2 -2
  74. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/_field.html.erb +41 -0
  75. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/_form.html.erb +16 -38
  76. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/_item.html.erb +10 -10
  77. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/_new.html.erb +1 -1
  78. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/_show.html.erb +15 -2
  79. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/_summary.html.erb +1 -1
  80. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/create.js.erb +8 -1
  81. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/index.js.erb +3 -3
  82. data/lib/rails/generators/humdrum/resource/templates/views/gumby/resource/update.js.erb +8 -1
  83. data/lib/rails/generators/humdrum/setup/setup_generator.rb +57 -12
  84. data/lib/rails/generators/humdrum/setup/templates/config/initializers/config_center.rb +71 -0
  85. data/lib/rails/generators/humdrum/{resource → setup}/templates/config/locales/humdrum.en.yml +0 -0
  86. data/lib/rails/generators/humdrum/setup/templates/ruby-gemset +1 -1
  87. data/lib/rails/generators/humdrum/setup/templates/ruby-version +1 -1
  88. metadata +54 -3
@@ -1,4 +1,4 @@
1
1
  module Humdrum
2
2
  # http://semver.org/
3
- VERSION = "0.0.7"
3
+ VERSION = "0.0.8"
4
4
  end
@@ -0,0 +1,339 @@
1
+ require 'rails/generators'
2
+ require 'rails/generators/migration'
3
+
4
+ module Humdrum
5
+ module Generators
6
+ class ResourceGenerator < Rails::Generators::Base
7
+
8
+ include Rails::Generators::Migration
9
+
10
+ def self.next_migration_number(path)
11
+ @migration_number = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i.to_s
12
+ end
13
+
14
+ source_root File.expand_path('../templates', __FILE__)
15
+
16
+ desc "Generates a controller, view and route entries for CRUD operations of a resource"
17
+
18
+ argument :resource_name, :type=>:string
19
+ argument :fields, :type=>:hash, :banner =>"Resource Fields."
20
+
21
+ class_option :fixed, :type => :boolean, :default => false, :desc => "Create fixed layouts. Default is fluid (Gumby doesn't support fluid framework. Hence applicable only for bootstrap for the timebeing.)"
22
+ class_option :debug, :type => :boolean, :default => false, :desc => "This will print the arguments for debugging"
23
+
24
+ class_option :front_end_framework, :type => :string, :default => 'bootstrap', :desc => "Support Twitter Bootstrap (twitter.github.io/bootstrap/) and Gumpy (http://gumbyframework.com/). Default is bootstrap. Pass gumby for Gumby Framework"
25
+
26
+ def debug_args
27
+ print_args if options.debug?
28
+ end
29
+
30
+ def generate_controllers
31
+ template "controllers/resource_controller.rb", "app/controllers/#{controller_path}_controller.rb"
32
+ end
33
+
34
+ def generate_views
35
+ template "views/#{options.front_end_framework}/resource/_edit.html.erb", "app/views/#{controller_path}/_edit.html.erb"
36
+ template "views/#{options.front_end_framework}/resource/_filters.html.erb", "app/views/#{controller_path}/_filters.html.erb"
37
+ template "views/#{options.front_end_framework}/resource/_field.html.erb", "app/views/#{controller_path}/_field.html.erb"
38
+ template "views/#{options.front_end_framework}/resource/_form.html.erb", "app/views/#{controller_path}/_form.html.erb"
39
+ template "views/#{options.front_end_framework}/resource/_index.html.erb", "app/views/#{controller_path}/_index.html.erb"
40
+ template "views/#{options.front_end_framework}/resource/_item.html.erb", "app/views/#{controller_path}/_item.html.erb"
41
+ template "views/#{options.front_end_framework}/resource/_nav_filters.html.erb", "app/views/#{controller_path}/_nav_filters.html.erb"
42
+ template "views/#{options.front_end_framework}/resource/_new.html.erb", "app/views/#{controller_path}/_new.html.erb"
43
+ template "views/#{options.front_end_framework}/resource/_show.html.erb", "app/views/#{controller_path}/_show.html.erb"
44
+ template "views/#{options.front_end_framework}/resource/_summary.html.erb", "app/views/#{controller_path}/_summary.html.erb"
45
+ template "views/#{options.front_end_framework}/resource/create.js.erb", "app/views/#{controller_path}/create.js.erb"
46
+ template "views/#{options.front_end_framework}/resource/destroy.js.erb", "app/views/#{controller_path}/destroy.js.erb"
47
+ template "views/#{options.front_end_framework}/resource/edit.js.erb", "app/views/#{controller_path}/edit.js.erb"
48
+ template "views/#{options.front_end_framework}/resource/index.js.erb", "app/views/#{controller_path}/index.js.erb"
49
+ template "views/#{options.front_end_framework}/resource/new.js.erb", "app/views/#{controller_path}/new.js.erb"
50
+ template "views/#{options.front_end_framework}/resource/show.js.erb", "app/views/#{controller_path}/show.js.erb"
51
+ template "views/#{options.front_end_framework}/resource/update.js.erb", "app/views/#{controller_path}/update.js.erb"
52
+ template "views/#{options.front_end_framework}/resource/index.html.erb", "app/views/#{controller_path}/index.html.erb"
53
+ end
54
+
55
+ def generate_models
56
+ template "models/resource.rb", "app/models/#{model_path}.rb"
57
+ end
58
+
59
+ def generate_javascript_validations
60
+ template "javascripts/validations/validator.js", "app/assets/javascripts/validations/#{model_path}.js"
61
+ end
62
+
63
+ def generate_migrations
64
+ migration_dir = "db/migrate"
65
+ migration_file_name = "create_#{instances_name}.rb"
66
+ destination = self.class.migration_exists?(migration_dir, migration_file_name)
67
+ if destination
68
+ say_status("skipped", "Migration #{migration_file_name}.rb already exists")
69
+ else
70
+ migration_template "migrations/create_resources.rb", "db/migrate/create_#{instances_name}.rb"
71
+ end
72
+ end
73
+
74
+ def generate_routes
75
+ words = name_phrases
76
+ resource = words.pop
77
+ route words.reverse.inject("resources :#{resource.pluralize}") { |acc, phrase|
78
+ "namespace(:#{phrase}){ #{acc} }"
79
+ }
80
+ end
81
+
82
+ private
83
+
84
+ def print_args
85
+ puts ":fields: #{fields}"
86
+ puts ":form_link_param: #{form_link_param}"
87
+ puts "name_phrases: #{name_phrases}"
88
+ puts "controller_path: #{controller_path}"
89
+ puts "controller_class: #{controller_class}"
90
+ puts "model_path: #{model_path}"
91
+ puts "model_class: #{model_class}"
92
+ puts "instance_name: #{instance_name}"
93
+ puts "instances_name: #{instances_name}"
94
+ puts "table_name: #{table_name}"
95
+
96
+ puts "index path: #{resource_link}"
97
+ puts "show path: #{resource_link('show')}"
98
+ puts "new path: #{resource_link('new')}"
99
+ puts "edit path: #{resource_link('edit')}"
100
+ puts "create path: #{resource_link('create')}"
101
+ puts "update path: #{resource_link('update')}"
102
+ puts "destroy path: #{resource_link('destroy')}"
103
+
104
+ puts "index url: #{resource_link('index','url')}"
105
+ puts "show url: #{resource_link('show','url')}"
106
+ puts "new url: #{resource_link('new','url')}"
107
+ puts "edit url: #{resource_link('edit','url')}"
108
+ puts "create url: #{resource_link('create','url')}"
109
+ puts "update url: #{resource_link('update','url')}"
110
+ puts "destroy url: #{resource_link('destroy','url')}"
111
+ end
112
+
113
+ def name_phrases
114
+ if resource_name.include?('::')
115
+ resource_name.split("::")
116
+ elsif resource_name.include?('/')
117
+ resource_name.split("/")
118
+ else
119
+ [resource_name]
120
+ end
121
+ end
122
+
123
+ def controller_path
124
+ words = name_phrases
125
+ resource = words.pop
126
+ if words.any?
127
+ words.collect(&:downcase).join("/") + "/#{resource.pluralize}"
128
+ else
129
+ "#{resource.pluralize}"
130
+ end
131
+ end
132
+
133
+ def controller_class
134
+ words = name_phrases
135
+ resource = words.pop
136
+ if words.any?
137
+ words.collect(&:camelize).join("::") + "::#{resource.camelize.pluralize}Controller"
138
+ else
139
+ "#{resource.camelize.pluralize}Controller"
140
+ end
141
+ end
142
+
143
+ def model_path
144
+ name_phrases.last.downcase
145
+ end
146
+
147
+ def model_class
148
+ name_phrases.last.camelize
149
+ end
150
+
151
+ def instance_name
152
+ name_phrases.last.underscore
153
+ end
154
+
155
+ def instances_name
156
+ instance_name.pluralize
157
+ end
158
+
159
+ def instance_title
160
+ instance_name.titleize
161
+ end
162
+
163
+ def instances_title
164
+ instances_name.titleize
165
+ end
166
+
167
+ def table_name
168
+ instances_name
169
+ end
170
+
171
+ def resource_link(actn='index', ltype='path')
172
+ map = {
173
+ 'index' => '',
174
+ 'show' => '',
175
+ 'edit' => 'edit_',
176
+ 'new' => 'new_',
177
+ 'update' => '',
178
+ 'create' => '',
179
+ 'destroy' => '',
180
+ }
181
+ words = name_phrases
182
+ resource = words.pop
183
+ if actn == "index"
184
+ map[actn] + (words.any? ? words.join("_" + "_") : "") + resource.pluralize + "_" + ltype
185
+ else
186
+ map[actn] + (words.any? ? words.join("_") + "_" : "") + resource + "_" + ltype
187
+ end
188
+ end
189
+
190
+ def form_link_param
191
+ words = name_phrases
192
+ resource = words.pop
193
+ if words.any?
194
+ # to print like this [:admin, :user, :location, @chakka]
195
+ # in form.html.erb
196
+ "[" + (words.map{|x| ":" + x.downcase} << "@" + resource.downcase).join(", ") + "]"
197
+ else
198
+ "@#{resource.downcase}"
199
+ end
200
+ end
201
+
202
+ ## List of all the string fields
203
+ def string_fields
204
+ main_field = main_string_field
205
+ fields.map{|name, type| name if name != main_field && type == "string" }.uniq.compact
206
+ end
207
+
208
+ ## List of all the string fields including main field
209
+ def string_fields_including_main_field
210
+ main_field = main_string_field
211
+ fields.map{|name, type| name if type == "string" }.uniq.compact
212
+ end
213
+
214
+ ## The main string field like 'name'
215
+ def main_string_field
216
+ fields.map{|name, type| name if name.include?("name") && type == "string"}.uniq.compact || fields.keys.any? ? fields.keys.first : "id"
217
+ end
218
+
219
+ def guess_input_type(name, type)
220
+ case type
221
+ when "string"
222
+ if name.include?("url")
223
+ return "url"
224
+ elsif name.include?("email")
225
+ return "email"
226
+ elsif name.include?("phone") || name.include?("mobile") || name.include?("landline") || name.include?("contact number")
227
+ return "tel"
228
+ elsif name.include?("time")
229
+ return "time"
230
+ elsif name.include?("date")
231
+ return "date"
232
+ elsif name.include?("password")
233
+ return "password"
234
+ else
235
+ "text"
236
+ end
237
+ when "text"
238
+ "textarea"
239
+ when "integer"
240
+ "number"
241
+ when "references"
242
+ "type"
243
+ when "date"
244
+ "date"
245
+ when "datetime"
246
+ "datetime-local"
247
+ when "timestamp", "time"
248
+ "time"
249
+ when "boolean"
250
+ "checkbox"
251
+ else
252
+ "text"
253
+ end
254
+ end
255
+
256
+ ## Text Fields like description or summary
257
+ def text_fields
258
+ fields.map{|name, type| name if type == "text"}.uniq.compact
259
+ end
260
+
261
+ def container_class
262
+ if options.front_end_framework == "bootstrap"
263
+ options.fixed? ? "container" : "container-fluid"
264
+ elsif options.front_end_framework == "gumby"
265
+ "container"
266
+ else
267
+ options.fixed? ? "container" : "container-fluid"
268
+ end
269
+ end
270
+
271
+ def row_class
272
+ if options.front_end_framework == "bootstrap"
273
+ options.fixed? ? "row" : "row-fluid"
274
+ elsif options.front_end_framework == "gumby"
275
+ "row"
276
+ else
277
+ options.fixed? ? "row" : "row-fluid"
278
+ end
279
+ end
280
+
281
+ def column_class(grid_width_value)
282
+ grid_width_value_hash = {"1" => "one",
283
+ "2" => "two",
284
+ "3" => "three",
285
+ "4" => "four",
286
+ "5" => "five",
287
+ "6" => "six",
288
+ "7" => "seven",
289
+ "8" => "eight",
290
+ "9" => "nine",
291
+ "10" => "ten",
292
+ "11" => "eleven",
293
+ "12" => "twelve",
294
+ "13" => "thirteen",
295
+ "14" => "fourteen",
296
+ "15" => "fifteen",
297
+ "16" => "sixteen"}
298
+ if options.front_end_framework == "bootstrap"
299
+ "span#{grid_width_value}"
300
+ elsif options.front_end_framework == "gumby"
301
+ "#{grid_width_value_hash[grid_width_value.to_s]} columns"
302
+ else
303
+ "col#{grid_width_value}"
304
+ end
305
+ end
306
+
307
+ # This function is not used now.
308
+ def button_class(color, size, rounded="", pretty="")
309
+ if options.front_end_framework == "bootstrap"
310
+ color_hash = {"blue" => "btn-primary",
311
+ "light-blue" => "btn-info",
312
+ "grey" => "",
313
+ "black" => "btn-inverse",
314
+ "red" => "btn-danger",
315
+ "green" => "btn-success",
316
+ "orange" => "btn-warning"}
317
+ size_hash = {"large" => "btn-large",
318
+ "medium" => "",
319
+ "small" => "btn-tiny"}
320
+ elsif options.front_end_framework == "gumby"
321
+ color_hash = {"blue" => "primary",
322
+ "dark-green" => "secondary",
323
+ "grey" => "default",
324
+ "black" => "info",
325
+ "red" => "danger",
326
+ "green" => "success",
327
+ "orange" => "warning"}
328
+ size_hash = {"xlarge" => "xlarge",
329
+ "large" => "large",
330
+ "medium" => "medium",
331
+ "small" => "small",
332
+ "green" => "btn-success"}
333
+ end
334
+ "#{pretty.blank? ? "" : "#{pretty} "}#{rounded.blank? ? "" : "#{rounded} "}#{size_hash[size]} #{color_hash[color]} btn"
335
+ end
336
+
337
+ end
338
+ end
339
+ end
@@ -0,0 +1,207 @@
1
+ class <%= controller_class %> < ApplicationController
2
+
3
+ #before_filter :require_user, :set_navs, :parse_filters_from_url
4
+ layout "public"
5
+ #authorize_actions_for Item, :actions => {:index => :delete}
6
+
7
+ before_filter :parse_pagination_params, :only=>:index
8
+
9
+ # GET /<%= instances_name %>
10
+ # GET /<%= instances_name %>.js
11
+ # GET /<%= instances_name %>.json
12
+ def index
13
+
14
+ get_collections
15
+
16
+ respond_to do |format|
17
+ format.html # index.html.erb
18
+ format.json { render json: @<%= instances_name %> }
19
+ format.js {}
20
+ end
21
+ end
22
+
23
+ # GET /<%= instances_name %>/1
24
+ # GET /<%= instances_name %>/1.js
25
+ # GET /<%= instances_name %>/1.json
26
+ def show
27
+ ## Creating the <%= instance_name %> object
28
+ @<%= instance_name %> = <%= model_class %>.find(params[:id])
29
+
30
+ respond_to do |format|
31
+ format.html { get_collections and render :index }
32
+ format.json { render json: @<%= instance_name %> }
33
+ format.js {}
34
+ end
35
+ end
36
+
37
+ # GET /<%= instances_name %>/new
38
+ # GET /<%= instances_name %>/new.json
39
+ def new
40
+ ## Intitializing the <%= instance_name %> object
41
+ @<%= instance_name %> = <%= model_class %>.new
42
+
43
+ respond_to do |format|
44
+ format.html { get_collections and render :index }
45
+ format.json { render json: @<%= instance_name %> }
46
+ format.js {}
47
+ end
48
+ end
49
+
50
+ # GET /<%= instances_name %>/1/edit
51
+ def edit
52
+ ## Fetching the <%= instance_name %> object
53
+ @<%= instance_name %> = <%= model_class %>.find(params[:id])
54
+
55
+ respond_to do |format|
56
+ format.html { get_collections and render :index }
57
+ format.json { render json: @<%= instance_name %> }
58
+ format.js {}
59
+ end
60
+ end
61
+
62
+ # POST /<%= instances_name %>
63
+ # POST /<%= instances_name %>.js
64
+ # POST /<%= instances_name %>.json
65
+ def create
66
+ ## Creating the <%= instance_name %> object
67
+ @<%= instance_name %> = <%= model_class %>.new(params[:<%= instance_name %>])
68
+
69
+ ## Validating the data
70
+ @<%= instance_name %>.valid?
71
+
72
+ respond_to do |format|
73
+ if @<%= instance_name %>.errors.blank?
74
+
75
+ # Saving the <%= instance_name %> object
76
+ @<%= instance_name %>.save
77
+
78
+ # Setting the flash message
79
+ message = translate("forms.created_successfully", :item => "<%= instance_name.titleize %>")
80
+ store_flash_message(message, :success)
81
+
82
+ format.html {
83
+ redirect_to <%= resource_link('show','url') %>(@<%= instance_name %>), notice: message
84
+ }
85
+ format.json { render json: @<%= instance_name %>, status: :created, location: @<%= instance_name %> }
86
+ format.js {}
87
+ else
88
+
89
+ # Setting the flash message
90
+ message = @<%= instance_name %>.errors.full_messages.to_sentence
91
+ store_flash_message(message, :alert)
92
+
93
+ format.html { render action: "new" }
94
+ format.json { render json: @<%= instance_name %>.errors, status: :unprocessable_entity }
95
+ format.js {}
96
+ end
97
+ end
98
+ end
99
+
100
+ # PUT /<%= instances_name %>/1
101
+ # PUT /<%= instances_name %>/1.js
102
+ # PUT /<%= instances_name %>/1.json
103
+ def update
104
+ ## Fetching the <%= instance_name %>
105
+ @<%= instance_name %> = <%= model_class %>.find(params[:id])
106
+
107
+ ## Updating the @<%= instance_name %> object with params
108
+ @<%= instance_name %>.update_attributes(params[:<%= instance_name %>])
109
+
110
+ ## Validating the data
111
+ @<%= instance_name %>.valid?
112
+
113
+ respond_to do |format|
114
+ if @<%= instance_name %>.errors.blank?
115
+
116
+ # Saving the <%= instance_name %> object
117
+ @<%= instance_name %>.save
118
+
119
+ # Setting the flash message
120
+ message = translate("forms.updated_successfully", :item => "<%= instance_name.titleize %>")
121
+ store_flash_message(message, :success)
122
+
123
+ format.html {
124
+ redirect_to <%= resource_link('show','url') %>(@<%= instance_name %>), notice: message
125
+ }
126
+ format.json { head :no_content }
127
+ format.js {}
128
+
129
+ else
130
+
131
+ # Setting the flash message
132
+ message = @<%= instance_name %>.errors.full_messages.to_sentence
133
+ store_flash_message(message, :alert)
134
+
135
+ format.html {
136
+ render action: "edit"
137
+ }
138
+ format.json { render json: @<%= instance_name %>.errors, status: :unprocessable_entity }
139
+ format.js {}
140
+
141
+ end
142
+ end
143
+ end
144
+
145
+ # DELETE /<%= instances_name %>/1
146
+ # DELETE /<%= instances_name %>/1.js
147
+ # DELETE /<%= instances_name %>/1.json
148
+ def destroy
149
+ ## Fetching the <%= instance_name %>
150
+ @<%= instance_name %> = <%= model_class %>.find(params[:id])
151
+
152
+ respond_to do |format|
153
+ ## Destroying the <%= instance_name %>
154
+ @<%= instance_name %>.destroy
155
+
156
+ # Fetch the <%= instances_name %> to refresh ths list and details box
157
+ get_collections
158
+ @<%= instance_name %> = @<%= instances_name %>.first
159
+
160
+ # Setting the flash message
161
+ message = translate("forms.destroyed_successfully", :item => "<%= instance_name.titleize %>")
162
+ store_flash_message(message, :success)
163
+
164
+ format.html {
165
+ redirect_to <%= resource_link('index', 'url') %> notice: message
166
+ }
167
+ format.json { head :no_content }
168
+ format.js {}
169
+
170
+ end
171
+ end
172
+
173
+ private
174
+
175
+ def set_navs
176
+ set_nav("<%= model_class %>")
177
+ end
178
+
179
+ def get_collections
180
+ # Fetching the <%= instances_name %>
181
+ relation = <%= model_class %>.where("")
182
+ @filters = {}
183
+
184
+ if params[:<%= instance_name %>] && params[:<%= instance_name %>][:query]
185
+ @query = params[:<%= instance_name %>][:query].strip
186
+ if !@query.blank?
187
+ relation = relation.where("
188
+ <% string_fields_including_main_field.each_with_index do |sfield, i| -%>
189
+ <% if string_fields_including_main_field.size - 1 != i -%>
190
+ LOWER(<%= sfield %>) LIKE LOWER('%#{@query}%') OR\
191
+ <% else -%>
192
+ LOWER(<%= sfield %>) LIKE LOWER('%#{@query}%')")
193
+ <% end -%>
194
+ <% end -%>
195
+ end
196
+ end
197
+
198
+ @<%= instances_name %> = relation.order("created_at desc").page(@current_page).per(@per_page)
199
+
200
+ ## Initializing the @<%= instance_name %> object so that we can render the show partial
201
+ @<%= instance_name %> = @<%= instances_name %>.first unless @<%= instance_name %>
202
+
203
+ return true
204
+
205
+ end
206
+
207
+ end