Module: ActiveTrail::Controller

Defined in:
lib/active_trail/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
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
# File 'lib/active_trail/controller.rb', line 3

def self.included(base)
  base.class_eval do
    before_action :current_user_merge
  
    def current_user_merge
      if respond_to?(:current_user) && !current_user.nil?
        params[:current_user] = current_user
      end
    end
  end

  if base.instance_methods.include?(:collection)
    Trailblazer::Operation::Controller.instance_eval do
      begin
        # As activeadmin has a collection controller method we remove it
        # so we don't override original method and use `trb_collection`
        # unless respond_to?(:trb_collection)
        alias_method :trb_collection, :collection
        remove_method :collection
      rescue NameError
        # So you're doing a null rescue here, do you like it? NO!
        # But every modification on a AA related class, this class is reloaded
        # and was trying make the alias_method and raise this exception
        # don't know if its a rails issue, or my code issue
        # if someone knows how to fix without this (unpolite) nil rescue
        # please open an issue / PR
      end
    end
    base.include Trailblazer::Operation::Controller
  end
  require 'trailblazer/operation/controller/active_record'
  base.include Trailblazer::Operation::Controller::ActiveRecord # named instance variables.
end

Instance Method Details

#build_new_resourceObject



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/active_trail/controller.rb', line 56

def build_new_resource
  if params[:action] == 'create'
    run "#{trailblazer_operation_name}::Create".constantize do |op|
      return op.model
    end.else do |op|
      return op.contract
    end
  else
    form "#{trailblazer_operation_name}::Create".constantize do |op|
      return @contract
    end
  end
end

#collectionObject



37
38
39
40
41
42
# File 'lib/active_trail/controller.rb', line 37

def collection
  return get_collection_ivar if get_collection_ivar
  trb_collection "#{trailblazer_operation_name}::Index".constantize do |op|
    set_collection_ivar(@collection)
  end
end

#create_resource(object) ⇒ Object



70
71
72
73
74
# File 'lib/active_trail/controller.rb', line 70

def create_resource(object)
  run_create_callbacks object do
    return object
  end
end

#find_resourceObject



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/active_trail/controller.rb', line 44

def find_resource
  if params.has_key?(:id)
    run "#{trailblazer_operation_name}::Show".constantize do |op|
      return op.model
    end
  else
    form "#{trailblazer_operation_name}::Create".constantize do |op|
      return op.contract
    end
  end
end

#update(options = {}, &block) ⇒ Object



76
77
78
79
80
81
82
83
84
85
# File 'lib/active_trail/controller.rb', line 76

def update(options={}, &block)
  object = resource
  run "#{trailblazer_operation_name}::Update".constantize do |op|
    options[:location] ||= smart_resource_url
    respond_with(op.model, options)
  end.else do |op|
    instance_variable_set("@#{trailblazer_model_name}".to_sym, op.contract)
    respond_with(op, options)
  end
end