Module: Admin::ResourceHelpers

Extended by:
ActiveSupport::Concern
Included in:
BaseController
Defined in:
app/helpers/admin/resource_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_success_msg(now = true) ⇒ Object



89
90
91
92
93
94
95
96
# File 'app/helpers/admin/resource_helpers.rb', line 89

def add_success_msg(now = true)
  message = I18n.t('admin.common.save_success')
  if now
    flash.now[:admin_success] = message
  else
    flash[:admin_success] = message
  end
end

#apply(partial = :edit) ⇒ Object



33
34
35
36
37
38
39
40
# File 'app/helpers/admin/resource_helpers.rb', line 33

def apply(partial = :edit)
  resource.assign_attributes(resource_params)
  if resource.save
    redirect_after_save
  else
    render partial
  end
end

#build_resource(attributes = {}) ⇒ Object



73
74
75
# File 'app/helpers/admin/resource_helpers.rb', line 73

def build_resource(attributes = {})
  instance_variable_set("@#{resource_name}", resource_model.new(attributes))
end

#createObject



18
19
20
# File 'app/helpers/admin/resource_helpers.rb', line 18

def create
  apply(:new)
end

#destroyObject



28
29
30
31
# File 'app/helpers/admin/resource_helpers.rb', line 28

def destroy
  resource.destroy!
  redirect_to_last
end

#editObject



22
# File 'app/helpers/admin/resource_helpers.rb', line 22

def edit; end

#indexObject



12
13
14
# File 'app/helpers/admin/resource_helpers.rb', line 12

def index
  instance_variable_set("@#{resource_name.pluralize}", resource_model.order(created_at: :desc).paginate(page: params[:page], per_page: per_page))
end

#load_resource!Object



69
70
71
# File 'app/helpers/admin/resource_helpers.rb', line 69

def load_resource!
  instance_variable_set("@#{resource_name}", resource_model.find(params[:id]))
end

#newObject



16
# File 'app/helpers/admin/resource_helpers.rb', line 16

def new; end

#per_pageObject



98
99
100
# File 'app/helpers/admin/resource_helpers.rb', line 98

def per_page
  20
end

#redirect_after_save(options = {}) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'app/helpers/admin/resource_helpers.rb', line 51

def redirect_after_save(options = {})
  if params[:save].present?
    redirect_to_back
  else
    add_success_msg(false)
    path = options.fetch(:path, url_for(action: :edit, id: resource))
    redirect_to path
  end
end

#redirect_or_render(partial) ⇒ Object



42
43
44
45
46
47
48
49
# File 'app/helpers/admin/resource_helpers.rb', line 42

def redirect_or_render(partial)
  if params[:save].present?
    redirect_to_back
  else
    add_success_msg
    render partial
  end
end

#resourceObject



61
62
63
# File 'app/helpers/admin/resource_helpers.rb', line 61

def resource
  instance_variable_get("@#{resource_name}")
end

#resource_modelObject



77
78
79
# File 'app/helpers/admin/resource_helpers.rb', line 77

def resource_model
  resource_name.classify.constantize
end

#resource_nameObject



85
86
87
# File 'app/helpers/admin/resource_helpers.rb', line 85

def resource_name
  controller_name.singularize
end

#resource_paramsObject



81
82
83
# File 'app/helpers/admin/resource_helpers.rb', line 81

def resource_params
  params[resource_name.to_sym].permit!
end

#resourcesObject



65
66
67
# File 'app/helpers/admin/resource_helpers.rb', line 65

def resources
  instance_variable_get("@#{resource_name.pluralize}")
end

#updateObject



24
25
26
# File 'app/helpers/admin/resource_helpers.rb', line 24

def update
  apply
end