Class: Humpyard::AssetsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/humpyard/assets_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



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
# File 'app/controllers/humpyard/assets_controller.rb', line 19

def create  
  @asset = Humpyard::config.asset_types[params[:type]].new params[:asset]
        
  unless can? :create, @asset.asset
    render :json => {
      :status => :failed
    }, :status => 403
    return
  end
      
  if @asset.save
    @assets = Humpyard::Asset.all
    render :json => {
      :status => :ok,
      :replace => [{
        :element => "hy-asset-listview",
        :content => render_to_string(:partial => "list.html", :locals => {:assets => @assets, :asset => @asset})
      }],
      :flash => {
        :level => 'info',
        :content => I18n.t('humpyard_form.flashes.create_success', :model => Humpyard::Asset.model_name.human)
      }
    }
  else
    render :json => {
      :status => :failed, 
      :errors => @asset.errors,
      :flash => {
        :level => 'error',
        :content => I18n.t('humpyard_form.flashes.create_fail', :model => Humpyard::Asset.model_name.human)
      }
    }
  end
end

#destroyObject



115
116
117
118
119
120
121
# File 'app/controllers/humpyard/assets_controller.rb', line 115

def destroy
  @asset = Humpyard::Asset.find(params[:id])
  
  authorize! :destroy, @asset  
  
  @asset.destroy
end

#editObject



54
55
56
57
58
59
60
# File 'app/controllers/humpyard/assets_controller.rb', line 54

def edit
  @asset = Humpyard::Asset.find(params[:id]).content_data
  
  authorize! :update, @asset.asset
  
  render :partial => 'edit'
end

#indexObject



3
4
5
6
7
# File 'app/controllers/humpyard/assets_controller.rb', line 3

def index
  @assets = Humpyard::Asset.all
  
  render :partial => 'index'
end

#newObject



9
10
11
12
13
14
15
16
17
# File 'app/controllers/humpyard/assets_controller.rb', line 9

def new     
  @asset = Humpyard::config.asset_types[params[:type]].new()
  
  authorize! :create, @asset.asset 
  
  @asset_type = params[:type]
  
  render :partial => 'edit'
end

#showObject



107
108
109
110
111
112
113
# File 'app/controllers/humpyard/assets_controller.rb', line 107

def show
  @asset = Humpyard::Asset.find(params[:id]).content_data
  
  authorize! :show, @asset.asset
  
  render :partial => 'show'
end

#updateObject



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
# File 'app/controllers/humpyard/assets_controller.rb', line 62

def update
  @asset = Humpyard::Asset.find(params[:id])
  if @asset 
    unless can? :update, @asset
      render :json => {
        :status => :failed
      }, :status => 403
      return
    end

    if @asset.content_data.update_attributes params[:asset]
      render :json => {
        :status => :ok,
        :replace => [
          { 
            :element => "hy-asset-listview-text-#{@asset.id}",
            :content => render_to_string(:partial =>'list_item.html', :locals => {:asset => @asset, :active => true})
          }
        ],
        :flash => {
          :level => 'info',
          :content => I18n.t('humpyard_form.flashes.update_success', :model => Humpyard::Asset.model_name.human)
        }
      }
    else
      render :json => {
        :status => :failed, 
        :errors => @asset.content_data.errors,
        :flash => {
          :level => 'error',
          :content => I18n.t('humpyard_form.flashes.update_fail', :model => Humpyard::Asset.model_name.human)
        }
      }
    end
  else
    render :json => {
      :status => :failed,
      :flash => {
        :level => 'error',
        :content => I18n.t('humpyard_form.flashes.not_found', :model => Humpyard::Asset.model_name.human)
      }
    }, :status => 404
  end
end