Top Level Namespace

Defined Under Namespace

Modules: Rails, SweetActions Classes: CollectAction, CreateAction, DestroyAction, ShowAction, SweetActionsController, UpdateAction

Instance Method Summary collapse

Instance Method Details

#create_sweet_action(method, path, action, options = {}) ⇒ Object



1
2
3
4
5
6
7
8
# File 'lib/sweet_actions/routes_helpers.rb', line 1

def create_sweet_action(method, path, action, options = {})
  to = options.fetch(:to, "sweet_actions##{action}")

  valid_methods = %i[get post put delete]
  raise 'not a valid method' unless valid_methods.include?(method.to_sym)
  args = { to: to }.merge(options)
  send(method, path, args)
end

#create_sweet_actions(resource, options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/sweet_actions/routes_helpers.rb', line 10

def create_sweet_actions(resource, options = {})
  path = options.fetch(:path, '/')
  namespace = options.fetch(:namespace, nil)
  singular = resource.to_s.singularize
  resource_class = singular.classify
  actions = %i[collect show create update destroy]
  only = options.fetch(:only, actions)
  base_path = "#{path}#{resource}"

  options = options.merge(resource_name: resource_class, namespace: namespace)

  if only.include?(:collect) || only.include?(:index)
    create_sweet_action(:get, base_path, :collect, options)
  end

  if only.include?(:show)
    create_sweet_action(:get, "#{base_path}/:id", :show, options)
  end

  if only.include?(:create)
    create_sweet_action(:post, base_path, :create, options)
  end

  if only.include?(:update)
    create_sweet_action(:put, "#{base_path}/:id", :update, options)
  end

  if only.include?(:destroy)
    create_sweet_action(:delete, "#{base_path}/:id", :destroy, options)
  end
end