Module: EasyAdminUi::CoreExt::ClassMethods

Defined in:
lib/easy_admin_ui/core_ext.rb

Instance Method Summary collapse

Instance Method Details

#easy_admin(admin_options = {}) ⇒ Object



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
131
132
133
134
135
136
137
# File 'lib/easy_admin_ui/core_ext.rb', line 8

def easy_admin(admin_options = {})
  self.easy_options = admin_options
  self.easy_options[:include] ||= ''

  make_resourceful do
    actions :all

    response_for :index do |format|
      format.html do
        @options = self.easy_options

        override = File.join('app/views', self.controller_path, 'index.html.erb')
        if @items.blank? && params[:page] && params[:page].to_i > 1
          redirect_to :action => :index
        else
          if File.exists?(override)
            render :template => override.gsub('app/views', '')
          else
            render :template => 'easy_admin_ui/index'
          end
        end
      end
    end

    response_for :show do |format|
      format.html do
        @options = self.easy_options

        override = File.join(controller_full_path, 'show.html.erb')
        if File.exists?(override)
          render :template => override.gsub('app/views', '')
        else
          render :template => 'easy_admin_ui/show'
        end
      end
    end

    [:new, :create_fails].each do |new_action|
      response_for new_action do |format|
        format.html do
          override = File.join(controller_full_path, 'new.html.erb')
          if File.exists?(override)
            render :template => override.gsub('app/views', '')
          else
            render :template => 'easy_admin_ui/new'
          end
        end
      end
    end

    response_for :create do |format|
      format.html do
        flash[:notice] = 'Created!'
        redirect_to :action => :index
      end
      format.js
    end

    [:edit, :update_fails].each do |edit_action|
      response_for edit_action do |format|
        format.html do
          override = File.join(controller_full_path, 'edit.html.erb')
          if File.exists?(override)
            render :template => override.gsub('app/views', '')
          else
            render :template => 'easy_admin_ui/edit'
          end
        end
      end
    end

    response_for :update do |format|
      format.html do
        flash[:notice] = 'Updated!'
        redirect_to :action => :index
      end
      format.js
    end

    response_for :destroy do |format|
      format.js do
        render :template => 'easy_admin_ui/destroy'
      end
    end

    eval(admin_options[:include])
  end

  class_eval do
    def object_parameters
      if request.post?
        if respond_to?(:allowed_params)
          return allowed_params
        elsif respond_to?(:create_params)
          return create_params
        end
      elsif request.patch? || request.put?
        if respond_to?(:allowed_params)
          return allowed_params
        elsif respond_to?(:update_params)
          return update_params
        end
      end

      {}
    end

    def current_objects
      if defined?(Mongoid) && current_model.included_modules.include?(Mongoid::Document)
        @items = current_model.send(easy_options[:order_method] ? easy_options[:order_method] : :asc, easy_options[:order]).page(params[:page]).per(easy_options[:per_page])
      else
        @items = current_model.order(easy_options[:order]).page(params[:page]).per(easy_options[:per_page])
      end
    end

    def current_object
      @item ||= current_model.find(params[:id]) if params[:id]
      @item ||= current_model.new(object_parameters)
    end

    def instance_variable_name
      'item'
    end

    helper_method :controller_full_path
    def controller_full_path
      File.join 'app/views', self.controller_path
    end
  end
end