gearhead 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (51) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +220 -0
  4. data/Rakefile +32 -0
  5. data/app/controllers/gearhead/application_controller.rb +5 -0
  6. data/app/controllers/gearhead/gears_controller.rb +98 -0
  7. data/config/routes.rb +9 -0
  8. data/lib/gearhead.rb +44 -0
  9. data/lib/gearhead/actions/create.rb +30 -0
  10. data/lib/gearhead/actions/index.rb +88 -0
  11. data/lib/gearhead/actions/show.rb +22 -0
  12. data/lib/gearhead/actions/update.rb +25 -0
  13. data/lib/gearhead/configuration.rb +68 -0
  14. data/lib/gearhead/engine.rb +10 -0
  15. data/lib/gearhead/extensions/actions.rb +29 -0
  16. data/lib/gearhead/extensions/associations.rb +13 -0
  17. data/lib/gearhead/extensions/attributes.rb +20 -0
  18. data/lib/gearhead/extensions/custom_actions.rb +36 -0
  19. data/lib/gearhead/extensions/enabled_actions.rb +9 -0
  20. data/lib/gearhead/extensions/finder.rb +14 -0
  21. data/lib/gearhead/extensions/pagination.rb +26 -0
  22. data/lib/gearhead/extensions/permitted_params.rb +36 -0
  23. data/lib/gearhead/extensions/querying.rb +13 -0
  24. data/lib/gearhead/extensions/scoping.rb +18 -0
  25. data/lib/gearhead/extensions/serialization.rb +21 -0
  26. data/lib/gearhead/gear.rb +56 -0
  27. data/lib/gearhead/gear_lookup.rb +35 -0
  28. data/lib/gearhead/gearbox.rb +83 -0
  29. data/lib/gearhead/paginators/lookup.rb +27 -0
  30. data/lib/gearhead/paginators/paginator.rb +48 -0
  31. data/lib/gearhead/paginators/pagy_paginator.rb +17 -0
  32. data/lib/gearhead/paginators/will_paginate_paginator.rb +13 -0
  33. data/lib/gearhead/params_builder.rb +14 -0
  34. data/lib/gearhead/registry.rb +26 -0
  35. data/lib/gearhead/resource_finder.rb +20 -0
  36. data/lib/gearhead/router.rb +51 -0
  37. data/lib/gearhead/serializers/active_model_serializers/invalid_request_serializer.rb +7 -0
  38. data/lib/gearhead/serializers/active_model_serializers/invalid_resource_serializer.rb +7 -0
  39. data/lib/gearhead/serializers/active_model_serializers/resource_serializer.rb +16 -0
  40. data/lib/gearhead/serializers/fast_jsonapi/invalid_request_serializer.rb +7 -0
  41. data/lib/gearhead/serializers/fast_jsonapi/invalid_resource_serializer.rb +7 -0
  42. data/lib/gearhead/serializers/fast_jsonapi/resource_serializer.rb +16 -0
  43. data/lib/gearhead/serializers/invalid_request_serializer.rb +24 -0
  44. data/lib/gearhead/serializers/invalid_resource_serializer.rb +59 -0
  45. data/lib/gearhead/serializers/lookup.rb +27 -0
  46. data/lib/gearhead/version.rb +3 -0
  47. data/lib/generators/gearhead/gear/gear_generator.rb +14 -0
  48. data/lib/generators/gearhead/gear/templates/gear.rb.erb +3 -0
  49. data/lib/generators/gearhead/install/install_generator.rb +17 -0
  50. data/lib/generators/gearhead/install/templates/gearhead.rb.erb +61 -0
  51. metadata +266 -0
@@ -0,0 +1,88 @@
1
+ module Gearhead
2
+ module Actions
3
+ class Index
4
+ delegate_missing_to :@request
5
+
6
+ def self.build(gear, request)
7
+ new(gear, request).build
8
+ end
9
+
10
+ attr_reader :scope, :gear, :request
11
+ def initialize(gear, request)
12
+ @gear = gear
13
+ @request = request
14
+ @collection = find_collection
15
+ @serializer_options = {}
16
+ end
17
+
18
+ def build
19
+ @collection = apply_includes
20
+ @collection = apply_scope
21
+ @collection = apply_query
22
+ @collection = apply_pagination
23
+ @collection = apply_serializer
24
+ @collection
25
+ end
26
+
27
+ private
28
+
29
+ def find_collection
30
+ @gear.resource.all
31
+ end
32
+
33
+ def apply_includes
34
+ return @collection if @gear._gear_includes.nil?
35
+ @collection.includes(@gear._gear_includes)
36
+ end
37
+
38
+ def apply_scope
39
+ return @collection unless valid_scope?(scope)
40
+ if query = @gear._gear_defined_scopes[scope.to_sym].presence
41
+ query.call(@collection)
42
+ else
43
+ @collection.send(scope)
44
+ end
45
+ end
46
+
47
+ def scope
48
+ params[:scope].presence || @gear._gear_default_scope
49
+ end
50
+
51
+ def valid_scope?(scope)
52
+ return false if scope.nil?
53
+ @gear._gear_defined_scopes.key?(scope.to_sym)
54
+ end
55
+
56
+ def apply_query
57
+ return @collection unless params[:q].present?
58
+
59
+ @collection.ransack(params[:q]).result
60
+ end
61
+
62
+ def apply_pagination
63
+ return @collection unless @gear.paginate?
64
+
65
+ paginator = ::Gearhead::Paginators::Paginator.paginate(@collection, self).call
66
+ @collection = paginator.collection
67
+ @serialization_options = paginator.serialization_options
68
+
69
+ @collection
70
+ end
71
+
72
+ def apply_serializer
73
+ @gear.serializer_class.new(@collection, serialization_options)
74
+ end
75
+
76
+ def serialization_options
77
+ {
78
+ links: {
79
+ first: @serialization_options[:first_url],
80
+ last: @serialization_options[:last_url],
81
+ prev: @serialization_options[:prev_url],
82
+ next: @serialization_options[:next_url]
83
+ }
84
+ }
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,22 @@
1
+ module Gearhead
2
+ module Actions
3
+ class Show
4
+ delegate_missing_to :@request
5
+
6
+ def self.build(gear, request, resource:)
7
+ new(gear, request, resource: resource).build
8
+ end
9
+
10
+ attr_reader :resource, :gear, :request
11
+ def initialize(gear, request, resource:)
12
+ @gear = gear
13
+ @request = request
14
+ @resource = resource
15
+ end
16
+
17
+ def build
18
+ @resource
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,25 @@
1
+ module Gearhead
2
+ module Actions
3
+ class Update
4
+ delegate_missing_to :@request
5
+
6
+ def self.build(gear, request, resource:)
7
+ new(gear, request, resource: resource).build
8
+ end
9
+
10
+ attr_reader :resource, :gear, :request
11
+ def initialize(gear, request, resource:)
12
+ @gear = gear
13
+ @request = request
14
+ @resource = resource
15
+ end
16
+
17
+ # todo
18
+ def build
19
+ params = ActionController::Parameters.new(@request.params).require(:post).permit(:person_id, :private)
20
+ @resource.assign_attributes(params)
21
+ @resource
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,68 @@
1
+ module Gearhead
2
+ # not implemented
3
+ class RansackConfiguration
4
+ attr_accessor :enabled, :predicates, :associations, :attributes, :scopes
5
+ def initialize
6
+ @enabled = true
7
+ @predicates = true
8
+ @associations = true
9
+ @attributes = true
10
+ @scopes = true
11
+ end
12
+
13
+ def enabled?
14
+ @enabled
15
+ end
16
+ end
17
+
18
+ class SerializationConfiguration
19
+ attr_accessor :adapter
20
+ def initialize
21
+ @adapter = :fast_jsonapi
22
+ end
23
+ end
24
+
25
+ class PaginationConfiguration
26
+ attr_accessor :adapter, :include_total, :per_page
27
+ def initialize
28
+ @adapter = :pagy
29
+ @enabled = true
30
+ @per_page = 30
31
+ end
32
+
33
+ def enabled?
34
+ @enabled
35
+ end
36
+ end
37
+
38
+ class AutomountConfiguration
39
+ attr_accessor :enabled, :excludes, :includes
40
+
41
+ def initialize
42
+ @enabled = false
43
+ @excludes = []
44
+ @includes = []
45
+ end
46
+
47
+ def enabled?
48
+ @enabled
49
+ end
50
+ end
51
+
52
+ class Configuration
53
+ attr_accessor :actions, :current_user, :endpoint, :serializer, :scope, :ignored_params,
54
+ :automount, :pagination, :serialization, :base_controller
55
+
56
+ def initialize
57
+ @actions = [:index, :create, :show, :update, :destroy]
58
+ @current_user = nil
59
+ @scope = nil
60
+ @endpoint = "/gearhead"
61
+ @ignored_params = [:id, :created_at, :updated_at]
62
+ @automount = AutomountConfiguration.new
63
+ @pagination = PaginationConfiguration.new
64
+ @serialization = SerializationConfiguration.new
65
+ @base_controller = 'ApplicationController'
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,10 @@
1
+ module Gearhead
2
+ class Engine < Rails::Engine
3
+ config.to_prepare do
4
+ Dir.glob(Rails.root.join("app/gears/**/*.rb")).each do |gear|
5
+ load gear
6
+ end
7
+ Rails.autoloaders.main.ignore(Rails.root.join('app/gears'))
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,29 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Actions
4
+ def self.included(klass)
5
+ klass.define_gear_setting :enabled_actions, Gearhead.config.actions.map(&:to_sym)
6
+ end
7
+
8
+ def disable_actions(*actions)
9
+ @_gear_enabled_actions -= actions.map(&:to_sym)
10
+ end
11
+
12
+ def actions(*args)
13
+ options = args.extract_options!
14
+ @_gear_enabled_actions = if options[:except]
15
+ @_gear_enabled_actions - options[:except].map(&:to_sym)
16
+ elsif options[:only]
17
+ options[:only].map(&:to_sym)
18
+ else
19
+ args.map(&:to_sym)
20
+ end
21
+ end
22
+
23
+ def action_enabled?(action)
24
+ @_gear_enabled_actions.include?(action)
25
+ end
26
+
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,13 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Associations
4
+ def self.included(klass)
5
+ klass.define_gear_setting :associations, { belongs_to: [], has_many: [], has_one: [] }
6
+ end
7
+
8
+ def belongs_to(klass)
9
+ @_gear_associations[:belongs_to] << klass
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,20 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Attributes
4
+ def self.included(klass)
5
+ end
6
+
7
+ def _gear_attributes
8
+ @_gear_attributes
9
+ end
10
+
11
+ def attributes(*attrs)
12
+ @_gear_attributes = *attrs
13
+ end
14
+
15
+ def default_attributes
16
+ @resource.columns_hash.keys.map(&:to_sym)
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,36 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module CustomActions
4
+ def self.included(klass)
5
+ klass.define_gear_setting :collection_actions, {}
6
+ klass.define_gear_setting :member_actions, {}
7
+ end
8
+
9
+ class CustomAction
10
+ attr_reader :name, :verbs, :block
11
+ def initialize(name, verbs, &block)
12
+ @name = name
13
+ @verbs = verbs
14
+ @block = block
15
+ end
16
+ end
17
+
18
+ def custom_action(type, name, options = {}, &block)
19
+ vias = (Array(options[:via]).presence || [:get]).map { |via| via.downcase.to_sym }
20
+ if type == :member
21
+ @_gear_member_actions[name] = CustomAction.new(name, vias, &block)
22
+ else
23
+ @_gear_collection_actions[name] = CustomAction.new(name, vias, &block)
24
+ end
25
+ end
26
+
27
+ def member_action(name, options = {}, &block)
28
+ custom_action(:member, name, options, &block)
29
+ end
30
+
31
+ def collection_action(name, options = {}, &block)
32
+ custom_action(:collection, name, options, &block)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,9 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module EnabledActions
4
+ def finder(&block)
5
+ @_gear_finder = block
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Finder
4
+ def self.included(klass)
5
+ klass.define_gear_setting :finder, nil
6
+ klass.define_gear_setting :param_key, :id
7
+ end
8
+
9
+ def finder(&block)
10
+ @_gear_finder = block
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Pagination
4
+ def self.included(klass)
5
+ klass.define_gear_setting :paginate, Gearhead.config.pagination.enabled?
6
+ klass.define_gear_setting :per_page, Gearhead.config.pagination.per_page
7
+ end
8
+
9
+ def paginate?
10
+ @_gear_paginate === true
11
+ end
12
+
13
+ def paginate(val)
14
+ @_gear_paginate = val === true
15
+ end
16
+
17
+ def per_page(int)
18
+ @_gear_per_page = int
19
+ end
20
+
21
+ def pagination_total(boolean)
22
+ @_gear_pagination_total = boolean
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,36 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module PermittedParams
4
+ def self.included(klass)
5
+ klass.define_gear_setting :permitted_params, {}
6
+ klass.define_gear_setting :action_params, {}
7
+ end
8
+
9
+ def permit_params(*args)
10
+ options = args.extract_options!
11
+ if options[:only]
12
+ keys = Array(args[:only]).map(&:to_sym)
13
+ keys.each do |key|
14
+ @_gear_action_params[key] = args
15
+ end
16
+ else
17
+ @_gear_permitted_params = args
18
+ end
19
+ end
20
+
21
+ def permitted_attributes(action)
22
+ if attrs = @_gear_action_params[action.to_sym].presence
23
+ attrs
24
+ elsif attrs = @_gear_permitted_params.presence
25
+ attrs
26
+ else
27
+ @resource.columns_hash.keys.map(&:to_sym)
28
+ end
29
+ end
30
+
31
+ def permitted_params(action)
32
+ permitted_attributes(action)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,13 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Querying
4
+ def self.included(klass)
5
+ klass.define_gear_setting :includes, {}
6
+ end
7
+
8
+ def includes(*keys)
9
+ @_gear_includes = *keys
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module Gearhead
2
+ module Extensions
3
+ module Scoping
4
+ def self.included(klass)
5
+ klass.define_gear_setting :default_scope, nil
6
+ klass.define_gear_setting :defined_scopes, {}
7
+ end
8
+
9
+ def register_scope(name, query = nil)
10
+ @_gear_defined_scopes[name] = query
11
+ end
12
+
13
+ def default_scope(scope = nil)
14
+ @_gear_default_scope = scope
15
+ end
16
+ end
17
+ end
18
+ end