Class: Items

Inherits:
Controllers::App
  • Object
show all
Defined in:
app/controllers/items.rb

Direct Known Subclasses

Comments

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.model_classObject

model_class



203
204
205
# File 'app/controllers/items.rb', line 203

def self.model_class
  @model_class ||= "Models::#{self.alias.singularize}".constantize
end

Instance Method Details

#allObject



110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/controllers/items.rb', line 110

def all
  respond_to do |f|
    @page, @per_page = (params.page || 1).to_i, Models::Item::PER_PAGE

    query = self.class.model_class.where(viewers: {_in: rad.user.major_roles}, dependent: {_exists: false})
    query = query.where tags: {_all: selected_tags} unless selected_tags.empty?
    @models = query.sort([:created_at, -1]).paginate(@page, @per_page).all

    f.html{@html_title = rad.config.title}
    f.json{render json: @models}
  end
end

#collaboratorsObject



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
# File 'app/controllers/items.rb', line 151

def collaborators
  require_permission :update_access, @model
  remove_roles = (params.remove_roles || '').split("\n")
  add_roles = (params.add_roles || '').split("\n")
  partials.show :context_menu

  remove_roles.each{|role| @model.remove_collaborator role}
  add_roles.each{|role| @model.add_collaborator role}

  respond_to do |format|
    if @model.save
      flash.info = t :collaborators_updated

      format.js{render action: :access} # "models/access"
      format.json{render :ok}
    else
      flash.info = t :failed

      format.js{render action: :access} # "models/access"
      format.json{render :failed}
    end
  end
end

#createObject



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'app/controllers/items.rb', line 58

def create
  require_permission :create
  @model = model_class.new params.model

  respond_to do |f|
    if @model.save
      flash.info = t :"#{model_class.alias.underscore}_created"

      f.js{redirect_to(path(@model))}
      f.json{render json: @model}
    else
      f.js{render action: :new}
      f.json{render json: {errors: @model.errors}, status: :failed}
    end
  end
end

#destroyObject



99
100
101
102
103
104
105
106
107
108
# File 'app/controllers/items.rb', line 99

def destroy
  require_permission :destroy, @model
  @model.destroy
  flash.info = t :"#{model_class.alias.underscore}_destroyed"

  respond_to do |f|
    f.js{redirect_to(default_path)}
    f.json{render :ok}
  end
end

#editObject



75
76
77
78
79
80
81
# File 'app/controllers/items.rb', line 75

def edit
  require_permission :update, @model

  respond_to do |f|
    f.js
  end
end

#layoutObject

Layout



179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/items.rb', line 179

def layout
  require_permission :update, @model

  @model.layout = params.value

  respond_to do |format|
    if @model.save
      flash.info = t :layout_updated

      format.js{reload_page}
      format.json{render :ok}
    else
      flash.info = t :failed

      format.js{}
      format.json{render :failed}
    end
  end
end

#model_classObject



206
# File 'app/controllers/items.rb', line 206

def model_class; self.class.model_class end

#newObject



48
49
50
51
52
53
54
55
56
# File 'app/controllers/items.rb', line 48

def new
  require_permission :create
  @model = model_class.new

  respond_to do |f|
    f.js
    f.json{render json: @model}
  end
end

#redirectObject



28
29
30
31
32
# File 'app/controllers/items.rb', line 28

def redirect
  url = rad.router.default_url
  url = url_for(:all) if url == '/' or url.blank?
  redirect_to url
end

#showObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/controllers/items.rb', line 34

def show
  respond_to do |f|
    if @model
      require_permission :view, @model

      f.html{@html_title = @model.name}
      f.json{render json: @model}
    else
      f.html{render :not_found}
      f.json{render :not_found}
    end
  end
end

#updateObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'app/controllers/items.rb', line 83

def update
  require_permission :update, @model

  respond_to do |f|
    if @model.set(params.model).save
      flash.info = t :"#{model_class.alias.underscore}_updated"

      f.js
      f.json{render :ok}
    else
      f.js{render action: :edit}
      f.json{render json: {errors: @model.errors}, status: :failed}
    end
  end
end

#viewersObject

Viewers and Collaborators



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'app/controllers/items.rb', line 127

def viewers
  require_permission :update_access, @model
  remove_roles = (params.remove_roles || '').split("\n")
  add_roles = (params.add_roles || '').split("\n")
  partials.show :context_menu

  remove_roles.each{|role| @model.remove_viewer role}
  add_roles.each{|role| @model.add_viewer role}

  respond_to do |format|
    if @model.save
      flash.info = t :viewers_updated

      format.js{render action: :access} # "models/access"
      format.json{render :ok}
    else
      flash.info = t :failed

      format.js{render action: :access} # "models/access"
      format.json{render :failed}
    end
  end
end