Class: Belt::NestedResourceBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/belt/route_dsl.rb

Instance Method Summary collapse

Constructor Details

#initialize(gateway, prefix, collection_prefix, inherited_tables: [], inherited_auth: nil, inherited_controller: nil) ⇒ NestedResourceBuilder

rubocop:disable Metrics/ParameterLists



54
55
56
57
58
59
60
61
62
# File 'lib/belt/route_dsl.rb', line 54

def initialize(gateway, prefix, collection_prefix, inherited_tables: [], inherited_auth: nil, # rubocop:disable Metrics/ParameterLists
               inherited_controller: nil)
  @gateway = gateway
  @prefix = prefix
  @collection_prefix = collection_prefix
  @inherited_tables = inherited_tables
  @inherited_auth = inherited_auth
  @inherited_controller = inherited_controller
end

Instance Method Details

#collectionObject



94
95
96
97
# File 'lib/belt/route_dsl.rb', line 94

def collection(&)
  MemberCollectionBuilder.new(@gateway, @collection_prefix, @inherited_tables,
                              @inherited_auth, @inherited_controller).instance_eval(&)
end

#memberObject



89
90
91
92
# File 'lib/belt/route_dsl.rb', line 89

def member(&)
  MemberCollectionBuilder.new(@gateway, @prefix, @inherited_tables, @inherited_auth,
                              @inherited_controller).instance_eval(&)
end

#resources(name, options = {}) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/belt/route_dsl.rb', line 64

def resources(name, options = {})
  resource_name = name.to_s
  singular = @gateway.send(:singularize, resource_name)
  param_name = options[:param] || "#{singular}_id"
  options = merge_inherited_options(options)
  options = @gateway.send(:auto_infer_tables, resource_name, options)
  resource_options = options.merge(route_type: :resources)
  actions = @gateway.send(:determine_actions, options)

  @gateway.send(:add_route, :get, "#{@prefix}/#{resource_name}", resource_options) if actions.include?(:index)
  @gateway.send(:add_route, :post, "#{@prefix}/#{resource_name}", resource_options) if actions.include?(:create)
  if actions.include?(:show)
    @gateway.send(:add_route, :get, "#{@prefix}/#{resource_name}/{#{param_name}}",
                  resource_options)
  end
  if actions.include?(:update)
    @gateway.send(:add_route, :put, "#{@prefix}/#{resource_name}/{#{param_name}}",
                  resource_options)
  end
  return unless actions.include?(:destroy)

  @gateway.send(:add_route, :delete, "#{@prefix}/#{resource_name}/{#{param_name}}",
                resource_options)
end