outpost-cms 0.0.3

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 (168) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.md +295 -0
  3. data/Rakefile +26 -0
  4. data/app/assets/images/glyphicons-halflings-red.png +0 -0
  5. data/app/assets/javascripts/outpost/application.js +1 -0
  6. data/app/assets/javascripts/outpost/auto_slug_field.js.coffee +53 -0
  7. data/app/assets/javascripts/outpost/base.js.coffee +1 -0
  8. data/app/assets/javascripts/outpost/date_time_input.js.coffee +108 -0
  9. data/app/assets/javascripts/outpost/field_counter.js.coffee +93 -0
  10. data/app/assets/javascripts/outpost/field_manager.js.coffee +37 -0
  11. data/app/assets/javascripts/outpost/global_plugins.js.coffee +87 -0
  12. data/app/assets/javascripts/outpost/index_manager.js.coffee +88 -0
  13. data/app/assets/javascripts/outpost/notification.js.coffee +46 -0
  14. data/app/assets/javascripts/outpost/preview.js.coffee +60 -0
  15. data/app/assets/javascripts/outpost/templates/date_field.jst.eco +3 -0
  16. data/app/assets/javascripts/outpost/templates/loading.jst.eco +11 -0
  17. data/app/assets/javascripts/outpost/templates/slug_generate_button.jst.eco +1 -0
  18. data/app/assets/javascripts/outpost/templates/time_field.jst.eco +3 -0
  19. data/app/assets/javascripts/outpost/templates.js +1 -0
  20. data/app/assets/javascripts/outpost.js +32 -0
  21. data/app/assets/stylesheets/outpost/_base.css.scss +127 -0
  22. data/app/assets/stylesheets/outpost/_edit.css.scss +13 -0
  23. data/app/assets/stylesheets/outpost/_forms.css.scss +116 -0
  24. data/app/assets/stylesheets/outpost/_index.css.scss +68 -0
  25. data/app/assets/stylesheets/outpost/_utility.css.scss +16 -0
  26. data/app/assets/stylesheets/outpost/application.css.scss +1 -0
  27. data/app/assets/stylesheets/outpost/bootstrap/bootstrap.css.scss +49 -0
  28. data/app/assets/stylesheets/outpost/bootstrap/datepicker.css.scss +301 -0
  29. data/app/assets/stylesheets/outpost.css.scss +14 -0
  30. data/app/controllers/outpost/application_controller.rb +40 -0
  31. data/app/controllers/outpost/base_controller.rb +3 -0
  32. data/app/controllers/outpost/errors_controller.rb +9 -0
  33. data/app/controllers/outpost/home_controller.rb +2 -0
  34. data/app/controllers/outpost/resource_controller.rb +12 -0
  35. data/app/controllers/outpost/sessions_controller.rb +36 -0
  36. data/app/helpers/authorization_helper.rb +44 -0
  37. data/app/helpers/list_helper.rb +243 -0
  38. data/app/helpers/outpost_helper.rb +49 -0
  39. data/app/helpers/render_helper.rb +41 -0
  40. data/app/helpers/utility_helper.rb +136 -0
  41. data/app/inputs/date_time_input.rb +12 -0
  42. data/app/models/permission.rb +18 -0
  43. data/app/models/user_permission.rb +4 -0
  44. data/app/views/kaminari/bootstrap/_first_page.html.erb +3 -0
  45. data/app/views/kaminari/bootstrap/_gap.html.erb +3 -0
  46. data/app/views/kaminari/bootstrap/_last_page.html.erb +3 -0
  47. data/app/views/kaminari/bootstrap/_next_page.html.erb +3 -0
  48. data/app/views/kaminari/bootstrap/_page.html.erb +3 -0
  49. data/app/views/kaminari/bootstrap/_paginator.html.erb +17 -0
  50. data/app/views/kaminari/bootstrap/_prev_page.html.erb +3 -0
  51. data/app/views/layouts/outpost/application.html.erb +101 -0
  52. data/app/views/layouts/outpost/minimal.html.erb +26 -0
  53. data/app/views/outpost/errors/error_404.html.erb +1 -0
  54. data/app/views/outpost/errors/error_500.html.erb +8 -0
  55. data/app/views/outpost/home/dashboard.html.erb +1 -0
  56. data/app/views/outpost/resource/_errors.html.erb +11 -0
  57. data/app/views/outpost/resource/_extra_fields.html.erb +1 -0
  58. data/app/views/outpost/resource/_form_fields.html.erb +9 -0
  59. data/app/views/outpost/resource/edit.html.erb +44 -0
  60. data/app/views/outpost/resource/index.html.erb +22 -0
  61. data/app/views/outpost/resource/new.html.erb +21 -0
  62. data/app/views/outpost/resource/search.html.erb +1 -0
  63. data/app/views/outpost/resource/show.html.erb +1 -0
  64. data/app/views/outpost/sessions/new.html.erb +16 -0
  65. data/app/views/outpost/shared/_add_link.html.erb +1 -0
  66. data/app/views/outpost/shared/_breadcrumbs.html.erb +15 -0
  67. data/app/views/outpost/shared/_cancel_link.html.erb +1 -0
  68. data/app/views/outpost/shared/_columns.html.erb +5 -0
  69. data/app/views/outpost/shared/_filters.html.erb +16 -0
  70. data/app/views/outpost/shared/_flash_messages.html.erb +6 -0
  71. data/app/views/outpost/shared/_form_block.html.erb +18 -0
  72. data/app/views/outpost/shared/_form_nav.html.erb +12 -0
  73. data/app/views/outpost/shared/_headers.html.erb +16 -0
  74. data/app/views/outpost/shared/_index_header.html.erb +4 -0
  75. data/app/views/outpost/shared/_list_table.html.erb +7 -0
  76. data/app/views/outpost/shared/_modal.html.erb +16 -0
  77. data/app/views/outpost/shared/_navigation.html.erb +31 -0
  78. data/app/views/outpost/shared/_notice.html.erb +1 -0
  79. data/app/views/outpost/shared/_pagination.html.erb +2 -0
  80. data/app/views/outpost/shared/_preview_errors.html.erb +9 -0
  81. data/app/views/outpost/shared/_submit_row.html.erb +50 -0
  82. data/config/routes.rb +4 -0
  83. data/lib/action_view/helpers/form_builder.rb +71 -0
  84. data/lib/outpost/breadcrumbs.rb +73 -0
  85. data/lib/outpost/config.rb +63 -0
  86. data/lib/outpost/controller/actions.rb +72 -0
  87. data/lib/outpost/controller/authentication.rb +34 -0
  88. data/lib/outpost/controller/authorization.rb +28 -0
  89. data/lib/outpost/controller/callbacks.rb +14 -0
  90. data/lib/outpost/controller/custom_errors.rb +41 -0
  91. data/lib/outpost/controller/filtering.rb +22 -0
  92. data/lib/outpost/controller/helpers.rb +52 -0
  93. data/lib/outpost/controller/ordering.rb +46 -0
  94. data/lib/outpost/controller/preferences.rb +71 -0
  95. data/lib/outpost/controller.rb +123 -0
  96. data/lib/outpost/engine.rb +10 -0
  97. data/lib/outpost/helpers/naming.rb +22 -0
  98. data/lib/outpost/helpers.rb +6 -0
  99. data/lib/outpost/hook.rb +35 -0
  100. data/lib/outpost/list/base.rb +78 -0
  101. data/lib/outpost/list/column.rb +24 -0
  102. data/lib/outpost/list/filter.rb +37 -0
  103. data/lib/outpost/list.rb +15 -0
  104. data/lib/outpost/model/authentication.rb +34 -0
  105. data/lib/outpost/model/authorization.rb +32 -0
  106. data/lib/outpost/model/identifier.rb +39 -0
  107. data/lib/outpost/model/methods.rb +23 -0
  108. data/lib/outpost/model/naming.rb +63 -0
  109. data/lib/outpost/model/routing.rb +138 -0
  110. data/lib/outpost/model/serializer.rb +27 -0
  111. data/lib/outpost/model.rb +22 -0
  112. data/lib/outpost/test.rb +21 -0
  113. data/lib/outpost/version.rb +3 -0
  114. data/lib/outpost-cms.rb +2 -0
  115. data/lib/outpost.rb +80 -0
  116. data/lib/tasks/outpost_tasks.rake +7 -0
  117. data/spec/controllers/authentication_spec.rb +62 -0
  118. data/spec/controllers/sessions_controller_spec.rb +99 -0
  119. data/spec/factories.rb +31 -0
  120. data/spec/helpers/authorization_helper_spec.rb +47 -0
  121. data/spec/helpers/list_helper_spec.rb +74 -0
  122. data/spec/helpers/outpost_helper_spec.rb +5 -0
  123. data/spec/helpers/render_helper_spec.rb +19 -0
  124. data/spec/helpers/utility_helper_spec.rb +53 -0
  125. data/spec/internal/app/controllers/application_controller.rb +3 -0
  126. data/spec/internal/app/controllers/outpost/people_controller.rb +23 -0
  127. data/spec/internal/app/controllers/outpost/pidgeons_controller.rb +5 -0
  128. data/spec/internal/app/controllers/people_controller.rb +9 -0
  129. data/spec/internal/app/controllers/pidgeons_controller.rb +3 -0
  130. data/spec/internal/app/models/person.rb +10 -0
  131. data/spec/internal/app/models/pidgeon.rb +3 -0
  132. data/spec/internal/app/models/post.rb +4 -0
  133. data/spec/internal/app/models/user.rb +4 -0
  134. data/spec/internal/app/views/people/index.html.erb +7 -0
  135. data/spec/internal/app/views/people/show.html.erb +1 -0
  136. data/spec/internal/config/database.yml +3 -0
  137. data/spec/internal/config/initializers/configuration.rb +3 -0
  138. data/spec/internal/config/initializers/outpost.rb +6 -0
  139. data/spec/internal/config/routes.rb +16 -0
  140. data/spec/internal/db/combustion_test.sqlite +0 -0
  141. data/spec/internal/db/schema.rb +44 -0
  142. data/spec/internal/db/seeds.rb +14 -0
  143. data/spec/internal/log/test.log +59277 -0
  144. data/spec/internal/public/favicon.ico +0 -0
  145. data/spec/lib/breadcrumbs_spec.rb +54 -0
  146. data/spec/lib/config_spec.rb +76 -0
  147. data/spec/lib/controller/actions_spec.rb +5 -0
  148. data/spec/lib/controller/authorization_spec.rb +4 -0
  149. data/spec/lib/controller/callbacks_spec.rb +31 -0
  150. data/spec/lib/controller/helpers_spec.rb +33 -0
  151. data/spec/lib/controller_spec.rb +25 -0
  152. data/spec/lib/helpers/naming_spec.rb +10 -0
  153. data/spec/lib/hook_spec.rb +13 -0
  154. data/spec/lib/list/base_spec.rb +96 -0
  155. data/spec/lib/list/column_spec.rb +46 -0
  156. data/spec/lib/list/filter_spec.rb +44 -0
  157. data/spec/lib/model/authentication_spec.rb +29 -0
  158. data/spec/lib/model/authorization_spec.rb +66 -0
  159. data/spec/lib/model/identifier_spec.rb +51 -0
  160. data/spec/lib/model/methods_spec.rb +8 -0
  161. data/spec/lib/model/naming_spec.rb +55 -0
  162. data/spec/lib/model/routing_spec.rb +166 -0
  163. data/spec/lib/model/serializer_spec.rb +13 -0
  164. data/spec/lib/outpost_spec.rb +34 -0
  165. data/spec/models/permission_spec.rb +10 -0
  166. data/spec/models/user_permission_spec.rb +4 -0
  167. data/spec/spec_helper.rb +22 -0
  168. metadata +411 -0
@@ -0,0 +1,72 @@
1
+ ##
2
+ # Outpost::Controller::Actions
3
+ #
4
+ # This provides basic CRUD actions for you to include into any
5
+ # controller that you want to behave like a resource management
6
+ # area.
7
+ module Outpost
8
+ module Controller
9
+ module Actions
10
+ def index
11
+ respond_with :outpost, @records
12
+ end
13
+
14
+ def new
15
+ breadcrumb "New"
16
+ @record = model.new
17
+ respond_with :outpost, @record
18
+ end
19
+
20
+ def show
21
+ redirect_to @record.admin_edit_path
22
+ end
23
+
24
+ def edit
25
+ breadcrumb "Edit", nil, @record.to_title
26
+ respond_with :outpost, @record
27
+ end
28
+
29
+ def create
30
+ @record = model.new(form_params)
31
+
32
+ if @record.save
33
+ notice "Saved #{@record.simple_title}"
34
+ respond_with :outpost, @record, location: requested_location
35
+ else
36
+ breadcrumb "New"
37
+ render :new
38
+ end
39
+ end
40
+
41
+ def update
42
+ if @record.update_attributes(form_params)
43
+ notice "Saved #{@record.simple_title}"
44
+ respond_with :outpost, @record, location: requested_location
45
+ else
46
+ breadcrumb "Edit", nil, @record.to_title
47
+ render :edit
48
+ end
49
+ end
50
+
51
+ def destroy
52
+ @record.destroy
53
+ notice "Deleted #{@record.simple_title}"
54
+ respond_with :outpost, @record
55
+ end
56
+
57
+ private
58
+
59
+ def form_params
60
+ params[model.singular_route_key]
61
+ end
62
+
63
+ def requested_location
64
+ case params[:commit_action]
65
+ when "edit" then @record.admin_edit_path
66
+ when "new" then model.admin_new_path
67
+ else model.admin_index_path
68
+ end
69
+ end
70
+ end # Actions
71
+ end # Controller
72
+ end # Outpost
@@ -0,0 +1,34 @@
1
+ module Outpost
2
+ module Controller
3
+ module Authentication
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ before_filter :require_login
8
+ helper_method :current_user
9
+ end
10
+
11
+ # Public: The currently logged-in user.
12
+ #
13
+ # Returns Outpost.user_class instance.
14
+ def current_user
15
+ begin
16
+ @current_user ||= Outpost.user_class.where(can_login: true).find(session[:user_id])
17
+ rescue ActiveRecord::RecordNotFound
18
+ session[:user_id] = nil
19
+ @current_user = nil
20
+ end
21
+ end
22
+
23
+ # Private: Callback to require login.
24
+ #
25
+ # Returns nothing.
26
+ def require_login
27
+ if !current_user
28
+ session[:return_to] = request.fullpath
29
+ redirect_to outpost_login_path and return false
30
+ end
31
+ end
32
+ end # Authentication
33
+ end # Controller
34
+ end # Outpost
@@ -0,0 +1,28 @@
1
+ ##
2
+ # Authorization
3
+ #
4
+ # Basic authorization methods for controllers
5
+ module Outpost
6
+ module Controller
7
+ module Authorization
8
+ # Make sure the user can authorize the current resource
9
+ def authorize(resource)
10
+ if !current_user.can_manage?(resource)
11
+ handle_unauthorized(resource)
12
+ end
13
+ end
14
+
15
+ # Use this for before_filter. Should be overridden for custom behavor.
16
+ def authorize_resource
17
+ authorize(self.class.model)
18
+ end
19
+
20
+ # What to do when a user doesn't have proper permissions
21
+ def handle_unauthorized(resource)
22
+ redirect_to outpost_root_path,
23
+ alert: "You don't have permission to manage #{resource.to_title.pluralize}"
24
+ return false
25
+ end
26
+ end # Authorization
27
+ end # Controller
28
+ end # Outpost
@@ -0,0 +1,14 @@
1
+ module Outpost
2
+ module Controller
3
+ module Callbacks
4
+ def get_record
5
+ @record = model.find(params[:id])
6
+ end
7
+
8
+ def get_records
9
+ @records = model.order("#{model.table_name}.#{order} #{sort_mode}")
10
+ .page(params[:page]).per(self.list.per_page)
11
+ end
12
+ end # Callbacks
13
+ end # Controller
14
+ end # Outpost
@@ -0,0 +1,41 @@
1
+ module Outpost
2
+ module Controller
3
+ module CustomErrors
4
+ extend ActiveSupport::Concern
5
+
6
+ NOT_FOUND_ERROR_CLASSES = [
7
+ ActionController::RoutingError,
8
+ ActionController::UnknownController,
9
+ AbstractController::ActionNotFound,
10
+ ActiveRecord::RecordNotFound
11
+ ]
12
+
13
+ if defined?(ActionController::UnknownFormat)
14
+ NOT_FOUND_ERROR_CLASSES << ActionController::UnknownFormat
15
+ end
16
+
17
+ included do
18
+ rescue_from StandardError, with: ->(e) { render_error(500, e) and return false }
19
+ rescue_from *NOT_FOUND_ERROR_CLASSES, with: ->(e) { render_error(404, e) and return false }
20
+ end
21
+
22
+ #----------------------
23
+
24
+ def render_error(status, e=StandardError)
25
+ response.status = status
26
+
27
+ if Rails.application.config.consider_all_requests_local
28
+ raise e
29
+ else
30
+ respond_to do |format|
31
+ format.html { render template: "/errors/error_#{status}", layout: "application", status: status, locals: { error: e } }
32
+ format.xml { render xml: { error: response.message, code: status }, status: status }
33
+ format.json { render json: { error: response.message, code: status }, status: status }
34
+ format.text { render text: "#{status} - #{response.message}", status: status}
35
+ format.any { head status }
36
+ end
37
+ end
38
+ end
39
+ end # CustomErrors
40
+ end
41
+ end
@@ -0,0 +1,22 @@
1
+ module Outpost
2
+ module Controller
3
+ module Filtering
4
+ private
5
+
6
+ def filter_records
7
+ if params[:filter].is_a? Hash
8
+ params[:filter].each do |attribute, value|
9
+ next if value.blank?
10
+ scope = "filtered_by_#{attribute}"
11
+
12
+ if @records.klass.respond_to? scope
13
+ @records = @records.send(scope, value)
14
+ else
15
+ @records = @records.where(attribute => value)
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,52 @@
1
+ ##
2
+ # Outpost::Controller::Helpers
3
+ module Outpost
4
+ module Controller
5
+ module Helpers
6
+ extend ActiveSupport::Concern
7
+ include Outpost::Controller::Ordering
8
+
9
+ included do
10
+ helper_method :sort_mode, :order
11
+ end
12
+
13
+ # Public: The order that the records are currently sorted by.
14
+ # This gets passed directly to ActiveRecord, so it should be
15
+ # a valid database order.
16
+ #
17
+ # Returns String of the order.
18
+ def order
19
+ @order ||= set_order
20
+ end
21
+
22
+ # Public: The sort mode that the records are currently sorted by.
23
+ # This gets passed directory to ActiveRecord, so it should be
24
+ # a valid database sort mode.
25
+ #
26
+ # Examples
27
+ #
28
+ # sort_mode
29
+ # # => "asc"
30
+ #
31
+ # Returns String of the sort mode.
32
+ def sort_mode
33
+ @sort_mode ||= set_sort_mode
34
+ end
35
+
36
+ # Public: Set the flash[:notice] message, only for HTML requests.
37
+ #
38
+ # message - (String) The message to add to the flash.
39
+ #
40
+ # Examples
41
+ #
42
+ # notice("Success")
43
+ # flash[:notice]
44
+ # # => "Success"
45
+ #
46
+ # Returns nothing.
47
+ def notice(message)
48
+ flash[:notice] = message if request.format.html?
49
+ end
50
+ end # Helpers
51
+ end # Controller
52
+ end # Outpost
@@ -0,0 +1,46 @@
1
+ module Outpost
2
+ module Controller
3
+ module Ordering
4
+ private
5
+
6
+ # Set which attribute is doing the sorting
7
+ #
8
+ # If params[:order] is present, then set the
9
+ # preference to that and return it.
10
+ #
11
+ # If not present, but preferred order is,
12
+ # then use the preferred order.
13
+ #
14
+ # Otherwise use the list order.
15
+ def set_order
16
+ key = "#{model.content_key}_order"
17
+ preferred_order = preference(key)
18
+
19
+ @order = if params[:order].present?
20
+ set_preference(key, params[:order])
21
+ elsif preferred_order.present?
22
+ preferred_order
23
+ else
24
+ list.default_order
25
+ end
26
+ end
27
+
28
+ # Set the sort mode
29
+ #
30
+ # It will either be the requested sort mode, or if not available,
31
+ # then the table's default sort mode.
32
+ def set_sort_mode
33
+ key = "#{model.content_key}_sort_mode"
34
+ preferred_sort_mode = preference(key)
35
+
36
+ @sort_mode = if %w{ asc desc }.include?(params[:sort_mode])
37
+ set_preference(key, params[:sort_mode])
38
+ elsif preferred_sort_mode.present?
39
+ preferred_sort_mode
40
+ else
41
+ list.default_sort_mode
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,71 @@
1
+ module Outpost
2
+ module Controller
3
+ module Preferences
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ helper_method :preference
8
+ end
9
+
10
+ # Public: Getter for preference.
11
+ # Preferences are stored in the browser's session on a per-resource
12
+ # basis.
13
+ #
14
+ # key - (String) The key for the preference.
15
+ #
16
+ # Examples
17
+ #
18
+ # preference("posts_order")
19
+ # # => "updated_at"
20
+ #
21
+ # Returns String of the requested preference.
22
+ def preference(key)
23
+ session["preference_#{key}"]
24
+ end
25
+
26
+ # Public: Writer for preference.
27
+ #
28
+ # key - (String) The key to write to.
29
+ # value - (String) The value of the preference.
30
+ #
31
+ # Examples
32
+ #
33
+ # set_preference("posts_order", "updated_at")
34
+ # preference("posts_order")
35
+ # # => "updated_at"
36
+ #
37
+ # Returns nothing.
38
+ def set_preference(key, value)
39
+ session["preference_#{key}"] = value
40
+ end
41
+
42
+ # Public: Unset a preference.
43
+ #
44
+ # key - (String) The key to delete from preferences.
45
+ #
46
+ # Examples
47
+ #
48
+ # unset_preference("posts_order")
49
+ # preference("posts_order")
50
+ # # => nil
51
+ #
52
+ # Returns nothing.
53
+ def unset_preference(key)
54
+ set_preference(key, nil)
55
+ end
56
+
57
+
58
+ private
59
+
60
+ def remove_preferences
61
+ Outpost.config.preferences.each do |preference|
62
+ key = "#{model.content_key}_#{preference}"
63
+
64
+ if params[preference] == "" && preference(key).present?
65
+ unset_preference(key)
66
+ end
67
+ end
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,123 @@
1
+ ##
2
+ # Outpost::Controller
3
+ module Outpost
4
+ module Controller
5
+ extend ActiveSupport::Autoload
6
+ extend ActiveSupport::Concern
7
+
8
+ autoload :Actions
9
+ autoload :Callbacks
10
+ autoload :Helpers
11
+ autoload :Ordering
12
+ autoload :Filtering
13
+ autoload :Preferences
14
+ autoload :Authentication
15
+ autoload :Authorization
16
+ autoload :CustomErrors
17
+
18
+ included do
19
+ helper_method :list, :model, :fields
20
+ end
21
+
22
+
23
+ # Public: Proxy to the controller's model.
24
+ def model
25
+ self.class.model
26
+ end
27
+
28
+ # Public: Proxy to the controller's list.
29
+ def list
30
+ self.class.list
31
+ end
32
+
33
+ # Public: Proxy to the controller's fields.
34
+ def fields
35
+ self.class.fields
36
+ end
37
+
38
+ module ClassMethods
39
+ attr_accessor :model
40
+ attr_writer :fields
41
+
42
+ # Public: The fields for a form.
43
+ #
44
+ # If no fields have been defined, then the default fields will be used.
45
+ # Using the default fields will also set the permitted params to those
46
+ # fields.
47
+ #
48
+ # Returns Array of fields.
49
+ def fields
50
+ @fields ||= begin
51
+ default_fields
52
+ end
53
+ end
54
+
55
+ # Public: The list for this controller.
56
+ #
57
+ # If no list has been defined yet, this method will
58
+ # also define the list with the class's default columns.
59
+ #
60
+ # Returns the list.
61
+ def list
62
+ @list ||= define_list do |l|
63
+ l.default_columns.each do |attribute|
64
+ l.column attribute
65
+ end
66
+ end
67
+ end
68
+
69
+ # Public: Define the list for this controller.
70
+ #
71
+ # block - A block to define the list. See List::Base for more.
72
+ #
73
+ # Examples
74
+ #
75
+ # define_list do |l|
76
+ # l.per_page = 50
77
+ # l.column :name
78
+ # end
79
+ #
80
+ # Returns nothing.
81
+ def define_list(&block)
82
+ @list = List::Base.new(model, &block)
83
+ end
84
+
85
+ # Public: Declare a controller as being a controller for Outpost.
86
+ #
87
+ # model - (constant) The model for this controller.
88
+ #
89
+ # Examples
90
+ #
91
+ # class Admin::NewsStoriesController < Admin::ResourceController
92
+ # outpost_controller model: NewsStory
93
+ # end
94
+ #
95
+ def outpost_controller(attributes={})
96
+ @model = attributes[:model] || find_model
97
+
98
+ include Outpost::Controller::Helpers
99
+ include Outpost::Controller::Callbacks
100
+ include Outpost::Controller::Actions
101
+ include Outpost::Controller::Ordering
102
+ include Outpost::Controller::Filtering
103
+ include Outpost::Controller::Preferences
104
+
105
+ before_filter :get_record, only: [:show, :edit, :update, :destroy]
106
+ before_filter :get_records, only: [:index]
107
+ before_filter :authorize_resource
108
+ before_filter :filter_records, only: [:index]
109
+ end
110
+
111
+
112
+ private
113
+
114
+ def default_fields
115
+ model.column_names - Outpost.config.excluded_form_fields
116
+ end
117
+
118
+ def find_model
119
+ self.name.demodulize.underscore.split("_")[0...-1].join("_").classify.constantize
120
+ end
121
+ end # ClassMethods
122
+ end # Controller
123
+ end # Outpost
@@ -0,0 +1,10 @@
1
+ module Outpost
2
+ class Engine < ::Rails::Engine
3
+ config.generators do |g|
4
+ g.test_framework :rspec, fixture: false
5
+ g.fixture_replacement :factory_girl, dir: "spec/factories"
6
+ g.assets false
7
+ g.helper false
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,22 @@
1
+ ##
2
+ # Naming
3
+ #
4
+ # Some naming helpers.
5
+ #
6
+ # Example:
7
+ #
8
+ # Outpost::Helpers::Naming.to_class("outpost/news_stories")
9
+ module Outpost
10
+ module Helpers
11
+ module Naming
12
+ extend self
13
+
14
+ # These helpers expect a controller param,
15
+ # such as 'outpost/news_stories'
16
+ # "outpost/news_stories" => NewsStory
17
+ def to_class(controller)
18
+ controller.singularize.camelize.demodulize.constantize
19
+ end
20
+ end # Naming
21
+ end # Helpers
22
+ end # Outpost
@@ -0,0 +1,6 @@
1
+ module Outpost
2
+ module Helpers
3
+ extend ActiveSupport::Autoload
4
+ autoload :Naming
5
+ end
6
+ end
@@ -0,0 +1,35 @@
1
+ ##
2
+ # Outpost::Hook
3
+ #
4
+ # Hook into Newsroom.js
5
+ module Outpost
6
+ class Hook
7
+ attr_accessor :data, :path
8
+
9
+ def initialize(options={})
10
+ @path = options[:path]
11
+ @data = options[:data] || { touch: true }
12
+ end
13
+
14
+ # Publish the message
15
+ def publish
16
+ response = connection.post do |request|
17
+ request.url @path
18
+ request.params = @data
19
+ end
20
+
21
+ response
22
+ end
23
+
24
+ private
25
+
26
+ def connection
27
+ @connection ||= begin
28
+ Faraday.new(url: Rails.application.config.node.server) do |conn|
29
+ conn.response :json
30
+ conn.adapter Faraday.default_adapter
31
+ end
32
+ end
33
+ end
34
+ end # Hook
35
+ end # Outpost
@@ -0,0 +1,78 @@
1
+ module Outpost
2
+ module List
3
+ class Base
4
+ def initialize(model, &block)
5
+ @model = model
6
+ @columns = ActiveSupport::HashWithIndifferentAccess.new
7
+ @filters = ActiveSupport::HashWithIndifferentAccess.new
8
+ @fields = []
9
+
10
+ yield self if block_given?
11
+
12
+ @default_order ||= List::DEFAULT_ORDER
13
+ @default_sort_mode ||= List::DEFAULT_SORT_MODE
14
+ @per_page ||= List::DEFAULT_PER_PAGE
15
+ end
16
+
17
+ attr_accessor :default_order, :default_sort_mode
18
+ attr_reader :columns, :fields, :filters, :per_page, :model
19
+
20
+ # Public: Set the per_page for pagination.
21
+ #
22
+ # val - (Integer) The value to send to pagination. Also accepts :all,
23
+ # which passes `nil` to pagination and therefore will not
24
+ # paginate.
25
+ #
26
+ # Returns nothing.
27
+ def per_page=(val)
28
+ @per_page = val.to_i
29
+ end
30
+
31
+ # Public: Add a column to the list.
32
+ #
33
+ # attribute - (String) The attribute that this column represents.
34
+ # options - (Hash) A hash of options. Gets passed directly to
35
+ # List::Column (default: {}).
36
+ # * header - (String) The title of the column, displayed in
37
+ # the table header
38
+ # (default: self.attribute.titleize).
39
+ # * display - (Symbol or Proc) How to display this attribute.
40
+ # * If symbol, should be the name of a method in
41
+ # AdminListHelper
42
+ # * If Proc, gets run as an instance of the class.
43
+ # * See AdminListHelper for more info.
44
+ #
45
+ # Examples
46
+ #
47
+ # define_list do
48
+ # column :name, header: "Full Name", display: :display_full_name
49
+ # column :user, header: "Associated User", display: proc { self.user.name }
50
+ # end
51
+ #
52
+ # Returns nothing.
53
+ def column(attribute, options={})
54
+ column = Column.new(attribute, self, options)
55
+ @columns[attribute] = column
56
+ end
57
+
58
+ # Public: Define a filter for the list.
59
+ #
60
+ # attribute - (String) The attribute on which to filter.
61
+ # options - (Hash) A hash of options that gets passed into Filter.new
62
+ # (default: {}).
63
+ #
64
+ # Returns nothing.
65
+ def filter(attribute, options={})
66
+ filter = Filter.new(attribute, self, options)
67
+ @filters[attribute] = filter
68
+ end
69
+
70
+ # Private: Default columns for this list
71
+ #
72
+ # Returns Array of default columns.
73
+ def default_columns
74
+ @model.column_names - Outpost.config.excluded_list_columns
75
+ end
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,24 @@
1
+ module Outpost
2
+ module List
3
+ class Column
4
+ attr_accessor :attribute, :display, :position, :list, :quick_edit,
5
+ :sortable, :default_sort_mode, :_display_helper, :header
6
+
7
+ alias_method :quick_edit?, :quick_edit
8
+ alias_method :sortable?, :sortable
9
+
10
+ def initialize(attribute, list, attributes={})
11
+ @attribute = attribute.to_s
12
+ @list = list
13
+ @position = @list.columns.size
14
+
15
+ @header = attributes[:header] || @attribute.titleize
16
+ @display = attributes[:display]
17
+ @quick_edit = !!attributes[:quick_edit]
18
+ @sortable = !!attributes[:sortable]
19
+
20
+ @default_sort_mode = attributes[:default_sort_mode] || List::DEFAULT_SORT_MODE
21
+ end
22
+ end
23
+ end
24
+ end