Module: DynamicController::InstanceMethods

Defined in:
lib/dynamic_controller/instance_methods.rb

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/dynamic_controller/instance_methods.rb', line 4

def self.included(base)
  base.rescue_from StandardError, with: :handle_error

  if base.include_action?(:index)
    base.send :define_method, :index do
      if parent_model
        model = parent_model.send(controller_name)
      else
        model = resource_class
      end

      self.collection =
          if search_query.is_a? String
            model.nql(search_query, distinct: true).page(params[:page])
          else
            model.search(search_query).result(distinct: true).page(params[:page])
          end

      Responder.new(self).index
    end
  end

  if base.include_action?(:show)
    base.send :define_method, :show do
      if parent_model
        self.model = parent_model.send(controller_name).find(params[:id])
      else
        self.model = resource_class.find(params[:id])
      end

      Responder.new(self).show
    end
  end

  if base.include_action?(:new)
    base.send :define_method, :new do
      if parent_model
        self.model = parent_model.send(controller_name).build
      else
        self.model = resource_class.new
      end

      Responder.new(self).new
    end
  end

  if base.include_action?(:edit)
    base.send :define_method, :edit do
      if parent_model
        self.model = parent_model.send(controller_name).find(params[:id])
      else
        self.model = resource_class.find(params[:id])
      end

      Responder.new(self).edit
    end
  end

  if base.include_action?(:create)
    base.send :define_method, :create do
      if parent_model
        self.model = parent_model.send(controller_name).build(params[controller_name.singularize])
      else
        self.model = resource_class.new(params[controller_name.singularize])
      end

      if model.save
        flash_message = "#{resource_class.model_name.human} successfully created"
        if params[:save_and_new]
          flash[:success] = flash_message
          redirect_to action: :new
        else
          flash.now[:success] = flash_message
          Responder.new(self).create
        end
      else
        respond_to do |format|
          format.html { render :new }
          format.json { render json: model.errors, status: :unprocessable_entity }
        end
      end
    end
  end

  if base.include_action?(:update)
    base.send :define_method, :update do
      if parent_model
        self.model = parent_model.send(controller_name).find(params[:id])
      else
        self.model = resource_class.find(params[:id])
      end

      if model.update_attributes(params[controller_name.singularize])
        flash.now[:success] = "#{resource_class.model_name.human} successfully updated"
        Responder.new(self).update
      else
        respond_to do |format|
          format.html { render :edit }
          format.json { render json: model.errors, status: :unprocessable_entity }
        end
      end
    end

  end

  if base.include_action?(:destroy)
    base.send :define_method, :destroy do
      if parent_model
        self.model = parent_model.send(controller_name).find(params[:id])
      else
        self.model = resource_class.find(params[:id])
      end

      if model.destroy
        flash[:warning] = "#{resource_class.model_name.human} successfully removed"
        Responder.new(self).destroy
      else
        flash[:danger] = "#{resource_class.model_name.human} could not be deleted"
        respond_to do |format|
          format.html { redirect_to action: :index }
          format.json { render json: model.errors, status: :unprocessable_entity }
        end
      end
    end
  end

end