Module: ResourceHelper

Included in:
Kuppayam::ResourceController
Defined in:
app/helpers/resource_helper.rb

Instance Method Summary collapse

Instance Method Details

#createObject



40
41
42
43
44
45
# File 'app/helpers/resource_helper.rb', line 40

def create
  @r_object = @resource_options[:class].new
  @r_object.assign_attributes(permitted_params)
  instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
  save_resource
end

#destroyObject



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
# File 'app/helpers/resource_helper.rb', line 72

def destroy
  get_resource
  if @r_object
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
    if @r_object.can_be_deleted?
      @r_object.destroy
      get_collections
      set_flash_message(I18n.t('success.deleted'), :success)
      set_notification(true, I18n.t('status.success'), I18n.t('success.deleted', item: default_item_name.titleize))
      @destroyed = true
    else
      message = I18n.t('errors.failed_to_delete', item: default_item_name.titleize)
      set_flash_message(message, :failure)
      set_notification(false, I18n.t('status.error'), message)
      @destroyed = false
    end
  else
    set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize))
  end
  
  respond_to do |format|
    format.html {}
    format.js  { 
      js_view_path = @resource_options && @resource_options[:js_view_path] ? "#{@resource_options[:js_view_path]}/destroy" : :destroy 
      render js_view_path
    }
  end

end

#editObject



30
31
32
33
34
35
36
37
38
# File 'app/helpers/resource_helper.rb', line 30

def edit
  get_resource
  if @r_object
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
  else
    set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize))
  end
  render_accordingly
end

#indexObject



3
4
5
6
7
8
9
10
11
12
# File 'app/helpers/resource_helper.rb', line 3

def index
  get_collections
  respond_to do |format|
    format.html {}
    format.js  { 
      js_view_path = @resource_options && @resource_options[:js_view_path] ? "#{@resource_options[:js_view_path]}/index" : :index 
      render js_view_path
    }
  end
end


118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/helpers/resource_helper.rb', line 118

def mark_as_featured
  get_resource
  if @r_object
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
    @r_object.update_attribute(:featured, true)
    if @r_object.errors.blank?
      set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: :featured))
    else
      set_notification(false, I18n.t('status.error'), @r_object.errors.full_messages.join("<br>"))
    end
  else
    set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
  end
  render_row
end

#newObject



24
25
26
27
28
# File 'app/helpers/resource_helper.rb', line 24

def new
  @r_object = @resource_options[:class].new
  instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
  render_accordingly
end


134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/helpers/resource_helper.rb', line 134

def remove_from_featured
  get_resource
  if @r_object
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
    @r_object.update_attribute(:featured, false)
    if @r_object.errors.blank?
      set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: :unfeatured))
    else
      set_notification(false, I18n.t('status.error'), @r_object.errors.full_messages.join("<br>"))
    end
  else
    set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
  end
  render_row
end

#showObject



14
15
16
17
18
19
20
21
22
# File 'app/helpers/resource_helper.rb', line 14

def show
  get_resource
  if @r_object
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
  else
    set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize))
  end
  render_accordingly
end

#updateObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'app/helpers/resource_helper.rb', line 47

def update
  get_resource
  if @r_object
    if @resource_options[:checkbox_fields] && @resource_options[:checkbox_fields].any?
      @resource_options[:checkbox_fields].each do |field|
        @r_object.write_attribute(field, false)
      end
    end
    @r_object.assign_attributes(permitted_params)
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
    if @r_object.can_be_edited?
      save_resource
    else
      message = I18n.t('errors.failed_to_update', item: default_item_name.titleize)
      @r_object.errors.add :base, message
      set_flash_message(message, :failure)
      set_notification(false, I18n.t('status.error'), message)
      render_accordingly
    end
  else
    set_notification(false, I18n.t('status.error'), I18n.t('status.not_found', item: default_item_name.titleize))
    render_accordingly
  end
end

#update_statusObject



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'app/helpers/resource_helper.rb', line 102

def update_status
  get_resource
  if @r_object
    instance_variable_set("@#{@resource_options[:item_name]}", @r_object)
    @r_object.update_attribute(:status, params[:status])
    if @r_object.errors.blank?
      set_notification(true, I18n.t('status.success'), I18n.t('state.changed', item: default_item_name.titleize, new_state: @r_object.status))
    else
      set_notification(false, I18n.t('status.error'), @r_object.errors.full_messages.join("<br>"))
    end
  else
    set_notification(false, I18n.t('status.not_found'), I18n.t('status.not_found', item: default_item_name.titleize))
  end
  render_row
end