kuppayam 0.1.15 → 0.1.16

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.
Files changed (47) hide show
  1. checksums.yaml +4 -4
  2. data/app/assets/javascripts/kuppayam/utilities.js +9 -0
  3. data/app/controllers/kuppayam/base_controller.rb +1 -0
  4. data/app/helpers/filter_helper.rb +4 -3
  5. data/app/helpers/image_helper.rb +7 -10
  6. data/app/helpers/navigation_helper.rb +11 -2
  7. data/app/helpers/resource_helper.rb +32 -0
  8. data/app/helpers/resource_view_helper.rb +227 -0
  9. data/app/models/concerns/approvable.rb +47 -0
  10. data/app/models/concerns/featureable.rb +23 -0
  11. data/app/models/concerns/publishable.rb +46 -0
  12. data/app/models/image/cover_image.rb +3 -0
  13. data/app/models/image/gallery_image.rb +11 -0
  14. data/app/models/image/logo_image.rb +3 -0
  15. data/app/models/image/profile_image.rb +3 -0
  16. data/app/uploaders/cover_image_uploader.rb +22 -0
  17. data/app/uploaders/gallery_image_uploader.rb +22 -0
  18. data/app/uploaders/logo_image_uploader.rb +22 -0
  19. data/app/uploaders/profile_image_uploader.rb +22 -0
  20. data/app/views/kuppayam/api/docs/_navbar.html.erb +12 -0
  21. data/app/views/kuppayam/api/docs/_navigation.html.erb +12 -19
  22. data/app/views/kuppayam/api/docs/show.html.erb +70 -80
  23. data/app/views/kuppayam/images/_form.html.erb +7 -1
  24. data/app/views/kuppayam/images/_multiple_images.html.erb +29 -0
  25. data/app/views/kuppayam/images/create.html.erb +20 -7
  26. data/app/views/kuppayam/images/destroy.js.erb +10 -2
  27. data/app/views/kuppayam/images/update.html.erb +2 -2
  28. data/app/views/layouts/dashboard/_items.html.erb +20 -0
  29. data/app/views/layouts/kuppayam/docs.html.erb +31 -55
  30. data/config/initializers/validators.rb +5 -1
  31. data/lib/generators/kuppayam/resource_generator.rb +254 -0
  32. data/lib/generators/kuppayam/templates/controllers/resource_controller.rb +84 -0
  33. data/lib/generators/kuppayam/templates/db/migrate/create_resources.rb +14 -0
  34. data/lib/generators/kuppayam/templates/models/resource.rb +72 -0
  35. data/lib/generators/kuppayam/templates/spec/controllers/resource_controller_spec.rb +154 -0
  36. data/lib/generators/kuppayam/templates/spec/factories/resource.rb +42 -0
  37. data/lib/generators/kuppayam/templates/spec/models/resource_spec.rb +36 -0
  38. data/lib/generators/kuppayam/templates/views/_form.html.erb +63 -0
  39. data/lib/generators/kuppayam/templates/views/_index.html.erb +53 -0
  40. data/lib/generators/kuppayam/templates/views/_row.html.erb +26 -0
  41. data/lib/generators/kuppayam/templates/views/_show.html.erb +112 -0
  42. data/lib/generators/kuppayam/templates/views/index.html.erb +46 -0
  43. data/lib/generators/resource/resource_generator.rb +337 -0
  44. data/lib/kuppayam/action_view/form_helper.rb +10 -2
  45. data/lib/kuppayam/action_view/theme_helper.rb +4 -2
  46. data/lib/kuppayam/version.rb +1 -1
  47. metadata +58 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aaebbb63581403b6e22fd770895774331d18509f
4
- data.tar.gz: af0d08e3b47b8342e032b08269321768603991b7
3
+ metadata.gz: 46f9d0794f3975e51fc8db814e72a716b6f4efc3
4
+ data.tar.gz: 803a98db92f7c41678ba29111f17ea8c84c5cfc8
5
5
  SHA512:
6
- metadata.gz: 624efb5ad7e4fd9e35fdf025b63f737a0315014fdcf24f35bd6169784210f59ecb6cf7dea71479bf141ec7727c138e88d8034fb095da6ab49ef6bec8b3b99648
7
- data.tar.gz: a546d268261790a0bc37a7ba700de938842066db788c3615d4615c183a8020cbc0748b2d2ffb33f4469043b9bb0ecc4effa44cbc3c07494da33f19a1ca167b3f
6
+ metadata.gz: 750dec101a2596eaf806373cabf8ae8c2de69d2566f1bef3b0f384075ddc06eeee7390e31db2f5a94fc5abf9271b0e493e4bdb85b6e686f118657c270eff56b4
7
+ data.tar.gz: d51130213fb36a466d05a3786dbf49b47ff34f7d999ecd3592daf07d7447a102e3081cf7d73acbdbbcf23b1788366413fa1d73da16e2380b000bc96332de47c4
@@ -187,3 +187,12 @@ function notifyInfo(title, message){
187
187
 
188
188
  toastr.info(message, title, opts);
189
189
  }
190
+
191
+ function convertToSlug(Text)
192
+ {
193
+ return Text
194
+ .toLowerCase()
195
+ .replace(/[^\w ]+/g,'')
196
+ .replace(/ +/g,'-')
197
+ ;
198
+ }
@@ -24,6 +24,7 @@ module Kuppayam
24
24
 
25
25
  include ImageHelper
26
26
  include FilterHelper
27
+ include ResourceViewHelper
27
28
 
28
29
  private
29
30
 
@@ -230,7 +230,8 @@ module FilterHelper
230
230
  filters_to_add: {},
231
231
  url_method_name: 'root_path',
232
232
  show_all_filter_on_top: true,
233
- show_null_filter_on_top: false
233
+ show_null_filter_on_top: false,
234
+ drop_down_options: {}
234
235
  )
235
236
 
236
237
  raise "@filter_param_mapping not initialised" unless @filter_param_mapping
@@ -269,7 +270,7 @@ module FilterHelper
269
270
  initial_list = {}
270
271
  initial_list["All"] = send(filter_options[:url_method_name], filters) if filter_options[:show_all_filter_on_top]
271
272
  initial_list["IS NOT SET"] = send(filter_options[:url_method_name], filters.merge({param_name => :null})) if filter_options[:show_null_filter_on_top]
272
-
273
+
273
274
  # Values can be either Array or Hash
274
275
  # If Array is passed, it will populate the Hash it want from the Hash
275
276
  # For e.g: if values = ["Jan", "Feb"], theh the hash populated would be {"Jan" => "Jan", "Feb" => "Feb"}
@@ -288,7 +289,7 @@ module FilterHelper
288
289
  results
289
290
  end
290
291
 
291
- drop_down_filter(selected_text, options)
292
+ drop_down_filter(selected_text, options, filter_options[:drop_down_options])
292
293
  end
293
294
 
294
295
  # Example
@@ -85,15 +85,7 @@ module ImageHelper
85
85
  class: ""
86
86
  )
87
87
 
88
- begin
89
- image_object_name = method_name.split(".").first
90
- image_object = object.send :eval, image_object_name
91
- image_object_id = image_object ? image_object.id : nil
92
- rescue
93
- raise "Error with display_image method in kuppayam image_helper.rb"
94
- end
95
-
96
- options[:class] = options[:class] + (object.persisted? ? " #{image_object_id}-#{options[:size]}-image" : "")
88
+ options[:class] = options[:class] + (object.persisted? ? " #{object.id}-#{options[:size]}-image" : "")
97
89
 
98
90
  img_url = image_url(object, method_name, **options)
99
91
  return image_tag(img_url, class: options[:class], width: options[:width], height: options[:height])
@@ -141,7 +133,7 @@ module ImageHelper
141
133
  remove_btn = link_to(remove_btn_display, delete_url, :class=>options[:delete_options][:class], :remote=>options[:delete_options][:remote], method: :delete)
142
134
  end
143
135
 
144
- link_to(image_tag, edit_url, :remote => options[:remote]) +
136
+ link_to(image_tag, edit_url, :remote => options[:edit_options][:remote]) +
145
137
  content_tag(:div, class: "btn-group btn-group-justified mt-30") do
146
138
  edit_btn + (object.persisted? ? remove_btn : "")
147
139
  end
@@ -174,6 +166,11 @@ module ImageHelper
174
166
  end
175
167
  end
176
168
 
169
+ def upload_multiple_image_link(object, assoc_name=:photo, scope=:admin, **options)
170
+ image_object = object.class.new.send("#{assoc_name}").build
171
+ main_app.url_for([:new, scope, :image, imageable_id: object.id, imageable_type: object.class.to_s, image_type: image_object.class.name, multiple: true])
172
+ end
173
+
177
174
  def remove_image_link(object, assoc_name=:photo, scope=:admin, **options)
178
175
  image_object = nil
179
176
  image_object = object.send(assoc_name) if object.respond_to?(assoc_name)
@@ -3,8 +3,17 @@ module NavigationHelper
3
3
  @nav == name
4
4
  end
5
5
 
6
- def nav_class(name)
7
- nav_active?(name) ? "active" : ""
6
+ def nav_include?(name)
7
+ @nav.include?(name)
8
+ end
9
+
10
+ # If check_include is passed, it check if the @nav has substring name else it should match
11
+ def nav_class(name, check_include=false)
12
+ if check_include
13
+ nav_include?(name) ? "active" : ""
14
+ else
15
+ nav_active?(name) ? "active" : ""
16
+ end
8
17
  end
9
18
 
10
19
  def set_nav(name)
@@ -110,6 +110,38 @@ module ResourceHelper
110
110
  render_row
111
111
  end
112
112
 
113
+ def mark_as_featured
114
+ @r_object = @resource_options[:class].find_by_id(params[:id])
115
+ if @r_object
116
+ instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
117
+ @r_object.update_attribute(:featured, true)
118
+ if @r_object.errors.blank?
119
+ set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: :featured))
120
+ else
121
+ set_notification(false, I18n.t('status.error'), @r_object.errors.full_messages.join("<br>"))
122
+ end
123
+ else
124
+ set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
125
+ end
126
+ render_row
127
+ end
128
+
129
+ def remove_from_featured
130
+ @r_object = @resource_options[:class].find_by_id(params[:id])
131
+ if @r_object
132
+ instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
133
+ @r_object.update_attribute(:featured, false)
134
+ if @r_object.errors.blank?
135
+ set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: :unfeatured))
136
+ else
137
+ set_notification(false, I18n.t('status.error'), @r_object.errors.full_messages.join("<br>"))
138
+ end
139
+ else
140
+ set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
141
+ end
142
+ render_row
143
+ end
144
+
113
145
  private
114
146
 
115
147
  def set_navs
@@ -0,0 +1,227 @@
1
+ module ResourceViewHelper
2
+
3
+ def edit_link(object)
4
+ url_for(action: 'edit', controller: object.class.to_s.tableize, id: object.id)
5
+ end
6
+
7
+ def delete_link(object)
8
+ url_for(action: 'destroy', controller: object.class.to_s.tableize, id: object.id)
9
+ end
10
+
11
+ def serial_number(i)
12
+ if i < 0
13
+ raw(content_tag(:i, "", class: "fa fa-check text-success"))
14
+ else
15
+ raw(i + 1 + (@per_page.to_i * (@current_page.to_i - 1)))
16
+ end
17
+ end
18
+
19
+ def display_thumbnail_small(object, **options)
20
+ options.reverse_merge!(
21
+ method_name: "cover_image.image.small.url",
22
+ image_class: "img-rectangle",
23
+ image_width: "120",
24
+ image_height: "auto",
25
+ )
26
+ url = url_for(action: 'show', controller: object.class.to_s.tableize, id: object.id)
27
+ link_to(url, remote: true) do
28
+ raw(display_image(object, options[:method_name], width: options[:image_width], height: options[:image_height], class: options[:image_class], alt: object.display_name))
29
+ end
30
+ end
31
+
32
+ def display_featured(object, **options)
33
+ options.reverse_merge!(
34
+ span_class: 'ml-5 mt-5 label',
35
+ )
36
+ if object.featured?
37
+ content_tag(:span, "Featured", class: "#{options[:span_class]} label-warning")
38
+ else
39
+ content_tag(:span, "Not Featured", class: "#{options[:span_class]} label-default")
40
+ end
41
+ end
42
+
43
+ def display_publishable_status(object, **options)
44
+ options.reverse_merge!(
45
+ span_class: 'ml-5 mt-5 label',
46
+ )
47
+ label_class = Publishable::STATUS_UI_CLASS[object.status]
48
+ content_tag(:span, object.display_status, class: "#{options[:span_class]} label-#{label_class}")
49
+ end
50
+
51
+ def display_publishable_links(object, **options)
52
+ options.reverse_merge!(
53
+ publish_icon: 'fa fa-check-square',
54
+ unpublish_icon: 'fa fa-square',
55
+ remove_icon: 'fa fa-close',
56
+ archive_icon: 'fa fa-archive',
57
+
58
+ publish_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "published"),
59
+ unpublish_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "unpublished"),
60
+ remove_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "removed"),
61
+ archive_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "archived"),
62
+
63
+ publish_text: "Publish",
64
+ unpublish_text: "UnPublish",
65
+ remove_text: "Remove",
66
+ archive_text: "Archive",
67
+
68
+ publish_class: "edit",
69
+ unpublish_class: "",
70
+ remove_class: "delete",
71
+ archive_class: "",
72
+
73
+ has_permission_to_edit: true
74
+ )
75
+
76
+ if @current_permission
77
+ options[:has_permission_to_edit] = @current_permission.can_update?
78
+ end
79
+
80
+ links = []
81
+ links << link_to(raw("<i class=\"#{options[:publish_icon]} mr-5\"></i> #{options[:publish_text]}"), options[:publish_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:publish_class]) if object.can_publish? && options[:has_permission_to_edit]
82
+ links << link_to(raw("<i class=\"#{options[:unpublish_icon]} mr-5\"></i> #{options[:unpublish_text]}"), options[:unpublish_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:unpublish_class]) if object.can_unpublish? && options[:has_permission_to_edit]
83
+ links << link_to(raw("<i class=\"#{options[:remove_icon]} mr-5\"></i> #{options[:remove_text]}"), options[:remove_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:remove_class]) if object.can_remove? && options[:has_permission_to_edit]
84
+ links << link_to(raw("<i class=\"#{options[:archive_icon]} mr-5\"></i> #{options[:archive_text]}"), options[:archive_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:archive_class]) if object.can_archive? && options[:has_permission_to_edit]
85
+ raw(links.join(""))
86
+ end
87
+
88
+ def display_featurable_links(object, **options)
89
+ options.reverse_merge!(
90
+ feature_icon: 'fa fa-star',
91
+ unfeature_icon: 'fa fa-star-o',
92
+
93
+ feature_link: url_for(action: 'mark_as_featured', controller: object.class.to_s.tableize, id: object.id),
94
+ unfeature_link: url_for(action: 'remove_from_featured', controller: object.class.to_s.tableize, id: object.id),
95
+
96
+ feature_text: "Mark as Featured",
97
+ unfeature_text: "Remove from Featured",
98
+
99
+ feature_class: "edit",
100
+ unfeature_class: "delete",
101
+
102
+ has_permission_to_edit: true
103
+ )
104
+
105
+ if @current_permission
106
+ options[:has_permission_to_edit] = @current_permission.can_update?
107
+ end
108
+
109
+ links = []
110
+ links << link_to(raw("<i class=\"#{options[:feature_icon]} mr-5\"></i> #{options[:feature_text]}"), options[:feature_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:feature_class]) unless object.featured? && options[:has_permission_to_edit]
111
+ links << link_to(raw("<i class=\"#{options[:unfeature_icon]} mr-5\"></i> #{options[:unfeature_text]}"), options[:unfeature_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:unfeature_class]) if object.featured? && options[:has_permission_to_edit]
112
+ raw(links.join(""))
113
+ end
114
+
115
+ def display_manage_links(object, current_user, **options)
116
+ options.reverse_merge!(
117
+ edit_icon: 'fa fa-edit',
118
+ delete_icon: 'fa fa-trash',
119
+
120
+ edit_link: url_for(action: "edit", controller: object.class.to_s.tableize, id: object.id),
121
+ delete_link: url_for(action: "destroy", controller: object.class.to_s.tableize, id: object.id),
122
+
123
+ edit_text: "Edit",
124
+ delete_text: "Delete",
125
+
126
+ edit_class: "edit",
127
+ delete_class: "",
128
+
129
+ has_permission_to_edit: true,
130
+ has_permission_to_delete: true
131
+ )
132
+
133
+ if @current_permission
134
+ options[:has_permission_to_edit] = @current_permission.can_update?
135
+ options[:has_permission_to_delete] = @current_permission.can_delete?
136
+ end
137
+
138
+ links = []
139
+ links << link_to(raw("<i class=\"#{options[:edit_icon]} mr-5\"></i> #{options[:edit_text]}"), options[:edit_link], remote: true, role: "menuitem", tabindex: "-1", class: options[:edit_class]) if object.can_be_edited? && options[:has_permission_to_edit]
140
+ links << link_to(raw("<i class=\"#{options[:delete_icon]} mr-5\"></i> #{options[:delete_text]}"), options[:delete_link], method: 'DELETE', remote: true, role: "menuitem", tabindex: "-1", class: options[:delete_class]) if object.can_be_deleted? && options[:has_permission_to_delete]
141
+ raw(links.join(""))
142
+ end
143
+
144
+ def display_publishable_buttons(object, **options)
145
+ options.reverse_merge!(
146
+ publish_icon: 'fa fa-check-square',
147
+ unpublish_icon: 'fa fa-square',
148
+ remove_icon: 'fa fa-close',
149
+ archive_icon: 'fa fa-archive',
150
+
151
+ publish_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "published"),
152
+ unpublish_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "unpublished"),
153
+ remove_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "removed"),
154
+ archive_link: url_for(action: 'update_status', controller: object.class.to_s.tableize, id: object.id, status: "archived"),
155
+
156
+ publish_text: "Publish",
157
+ unpublish_text: "UnPublish",
158
+ remove_text: "Remove",
159
+ archive_text: "Archive",
160
+
161
+ publish_class: "btn btn-block btn-success btn-only-hover",
162
+ unpublish_class: "btn btn-block btn-gray btn-only-hover",
163
+ remove_class: "btn btn-block btn-danger btn-only-hover",
164
+ archive_class: "btn btn-block btn-gray btn-only-hover",
165
+
166
+ has_permission_to_edit: true
167
+ )
168
+
169
+ if @current_permission
170
+ options[:has_permission_to_edit] = @current_permission.can_update?
171
+ end
172
+
173
+ links = []
174
+ links << link_to(raw("<i class=\"#{options[:publish_icon]} mr-5\"></i> #{options[:publish_text]}"), options[:publish_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:publish_class]) if object.can_publish? && options[:has_permission_to_edit]
175
+ links << link_to(raw("<i class=\"#{options[:unpublish_icon]} mr-5\"></i> #{options[:unpublish_text]}"), options[:unpublish_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:unpublish_class]) if object.can_unpublish? && options[:has_permission_to_edit]
176
+ links << link_to(raw("<i class=\"#{options[:remove_icon]} mr-5\"></i> #{options[:remove_text]}"), options[:remove_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:remove_class]) if object.can_remove? && options[:has_permission_to_edit]
177
+ links << link_to(raw("<i class=\"#{options[:archive_icon]} mr-5\"></i> #{options[:archive_text]}"), options[:archive_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:archive_class]) if object.can_archive? && options[:has_permission_to_edit]
178
+ raw(links.join(""))
179
+ end
180
+
181
+ def display_featurable_buttons(object, **options)
182
+ options.reverse_merge!(
183
+ feature_icon: 'fa fa-star',
184
+ unfeature_icon: 'fa fa-star-o',
185
+
186
+ feature_link: url_for(action: 'mark_as_featured', controller: object.class.to_s.tableize, id: object.id),
187
+ unfeature_link: url_for(action: 'remove_from_featured', controller: object.class.to_s.tableize, id: object.id),
188
+
189
+ feature_text: "Mark as Featured",
190
+ unfeature_text: "Remove from Featured",
191
+
192
+ feature_class: "btn btn-block btn-success btn-only-hover",
193
+ unfeature_class: "btn btn-block btn-danger btn-only-hover",
194
+
195
+ has_permission_to_edit: true
196
+ )
197
+
198
+ if @current_permission
199
+ options[:has_permission_to_edit] = @current_permission.can_update?
200
+ end
201
+
202
+ links = []
203
+ links << link_to(raw("<i class=\"#{options[:feature_icon]} mr-5\"></i> #{options[:feature_text]}"), options[:feature_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:feature_class]) unless object.featured? && options[:has_permission_to_edit]
204
+ links << link_to(raw("<i class=\"#{options[:unfeature_icon]} mr-5\"></i> #{options[:unfeature_text]}"), options[:unfeature_link], method: 'PUT', remote: true, role: "menuitem", tabindex: "-1", class: options[:unfeature_class]) if object.featured? && options[:has_permission_to_edit]
205
+ raw(links.join(""))
206
+ end
207
+
208
+ def display_manage_buttons(object, **options)
209
+ options.reverse_merge!(
210
+ edit_icon: 'fa fa-edit',
211
+ delete_icon: 'fa fa-trash',
212
+
213
+ edit_link: url_for(action: "edit", controller: object.class.to_s.tableize, id: object.id),
214
+ delete_link: url_for(action: "destroy", controller: object.class.to_s.tableize, id: object.id),
215
+
216
+ edit_text: "Edit",
217
+ delete_text: "Delete",
218
+
219
+ edit_class: "btn btn-block btn-success",
220
+ delete_class: "btn btn-block btn-danger btn-only-hover"
221
+ )
222
+ links = []
223
+ links << link_to(raw("<i class=\"#{options[:edit_icon]} mr-5\"></i> #{options[:edit_text]}"), options[:edit_link], remote: true, role: "menuitem", tabindex: "-1", class: options[:edit_class]) if object.can_be_edited?
224
+ links << link_to(raw("<i class=\"#{options[:delete_icon]} mr-5\"></i> #{options[:delete_text]}"), options[:delete_link], method: 'DELETE', remote: true, role: "menuitem", tabindex: "-1", class: options[:delete_class]) if object.can_be_deleted?
225
+ raw(links.join(""))
226
+ end
227
+ end
@@ -0,0 +1,47 @@
1
+ module Approvable
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ # Constants
6
+ APPROVED = "approved"
7
+ PENDING = "pending"
8
+ BLOCKED = "blocked"
9
+ SUSPENDED = "suspended"
10
+ REMOVED = "removed"
11
+
12
+ STATUS = {"Approved" => APPROVED, "Pending" => PENDING, "Suspended" => SUSPENDED, "Blocked" => BLOCKED, "Removed" => REMOVED}
13
+ STATUS_REVERSE = {APPROVED => "Approved", PENDING => "Pending", SUSPENDED => "Suspended", BLOCKED => "Blocked", REMOVED => "Removed"}
14
+
15
+ included do
16
+
17
+ validates :status, :presence=> true, :inclusion => {:in => STATUS_REVERSE.keys, :presence_of => :status, :message => "%{value} is not a valid status" }
18
+
19
+ state_machine :status, initial: PENDING do
20
+ event :approve do
21
+ transition [PENDING, SUSPENDED] => APPROVED
22
+ end
23
+ event :pending do
24
+ transition [APPROVED, SUSPENDED, BLOCKED, REMOVED] => PENDING
25
+ end
26
+ event :suspend do
27
+ transition [APPROVED, PENDING, BLOCKED] => SUSPENDED
28
+ end
29
+ event :block do
30
+ transition [APPROVED, PENDING, SUSPENDED] => BLOCKED
31
+ end
32
+ event :remove do
33
+ transition [APPROVED, PENDING, SUSPENDED, BLOCKED] => REMOVED
34
+ end
35
+ end
36
+
37
+ scope :approved, -> { where(status: APPROVED) }
38
+ scope :pending, -> { where(status: PENDING) }
39
+ scope :blocked, -> { where(status: BLOCKED) }
40
+ scope :suspended, -> { where(status: SUSPENDED) }
41
+ scope :removed, -> { where(status: REMOVED) }
42
+
43
+ scope :status, lambda { |status| where("LOWER(status)='#{status}'") }
44
+
45
+ end
46
+
47
+ end
@@ -0,0 +1,23 @@
1
+ module Featureable
2
+
3
+ extend ActiveSupport::Concern
4
+
5
+ # Constants
6
+ FEATURED = {"Featured" => true, "Non Featured" => false}
7
+ FEATURED_REVERSE = {true => "Featured", false => "Non Featured"}
8
+
9
+ included do
10
+
11
+ scope :featured, lambda { |val| where(featured: val) }
12
+
13
+ def mark_as_featured
14
+ self.update_attribute(:featured, true)
15
+ end
16
+
17
+ def remove_from_featured
18
+ self.update_attribute(:featured, false)
19
+ end
20
+
21
+ end
22
+
23
+ end