Class: Platform::Application

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
SimpleStringPermissions
Defined in:
app/models/platform/application.rb

Overview

– Copyright © 2011 Michael Berkovich

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ++

Constant Summary collapse

URL_REGEX =
/\Ahttp(s?):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/i

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SimpleStringPermissions

#has_permission?, #set_permission

Instance Attribute Details

#token_callback_urlObject

Returns the value of attribute token_callback_url.



60
61
62
# File 'app/models/platform/application.rb', line 60

def token_callback_url
  @token_callback_url
end

Class Method Details



364
365
366
# File 'app/models/platform/application.rb', line 364

def self.featured_for_category(category, page = 1, per_page = 20)
  self.where("platform_applications.state='approved' and platform_application_categories.category_id = ? and platform_application_categories.featured = ?", category.id, true).joins(:categories).order("platform_application_categories.position asc").page(page).per(per_page)
end

.find_token(token_key) ⇒ Object



105
106
107
108
109
110
111
112
# File 'app/models/platform/application.rb', line 105

def self.find_token(token_key)
  token = Platform::Oauth::OauthToken.find_by_token(token_key, :include => :application)
  if token && token.authorized?
    token
  else
    nil
  end
end

.for(client_id) ⇒ Object



100
101
102
103
# File 'app/models/platform/application.rb', line 100

def self.for(client_id)
  app = Platform::Application.find_by_id(client_id) if client_id.to_s.match(/^[\d]+$/)
  app || Platform::Application.find_by_key(client_id)
end

.permissionsObject



210
211
212
# File 'app/models/platform/application.rb', line 210

def self.permissions
  [:no_rate_limit, :grant_type_password, :unlimited_models, :add_without_premium]
end

.regular_for_category(category, page = 1, per_page = 20) ⇒ Object



368
369
370
# File 'app/models/platform/application.rb', line 368

def self.regular_for_category(category, page = 1, per_page = 20)
  self.where("platform_applications.state='approved' and platform_application_categories.category_id = ? and (platform_application_categories.featured is null or platform_application_categories.featured = ?)", category.id, false).joins(:categories).order("platform_application_categories.position asc").page(page).per(per_page)
end

Instance Method Details

#add_categories(catigories) ⇒ Object



344
345
346
347
348
# File 'app/models/platform/application.rb', line 344

def add_categories(catigories)
  catigories.each do |cat|
    add_category(cat)
  end
end

#add_category(cat) ⇒ Object



337
338
339
340
341
342
# File 'app/models/platform/application.rb', line 337

def add_category(cat)
  cat = Platform::Category.find_by_keyword(cat) if cat.is_a?(String)
  return nil if not cat
  
  Platform::ApplicationCategory.find_or_create(self, cat)
end


206
207
208
# File 'app/models/platform/application.rb', line 206

def admin_link
  "#{DEFAULT_SITE_LINK}/admin/applications/view/#{id}"
end

#allow_add_without_premium(value = true) ⇒ Object

Ticket 19507



172
173
174
# File 'app/models/platform/application.rb', line 172

def allow_add_without_premium(value=true)
  set_permission(:add_without_premium, value)
end

#allow_add_without_premium!(value = true) ⇒ Object

Ticket 19507



177
178
179
180
# File 'app/models/platform/application.rb', line 177

def allow_add_without_premium!(value=true)
  allow_add_without_premium(value)
  save!
end

#allow_add_without_premium?Boolean

Ticket 19507

Returns:

  • (Boolean)


183
184
185
# File 'app/models/platform/application.rb', line 183

def allow_add_without_premium?
  has_permission?(:add_without_premium)
end

#allow_grant_type_client_credentials?Boolean

Returns:

  • (Boolean)


151
152
153
# File 'app/models/platform/application.rb', line 151

def allow_grant_type_client_credentials?
  true # for now, all applications have a right to get client_token
end

#allow_grant_type_password(value = true) ⇒ Object

Ticket 19135



136
137
138
# File 'app/models/platform/application.rb', line 136

def allow_grant_type_password(value=true)
  set_permission(:grant_type_password, value)
end

#allow_grant_type_password!(value = true) ⇒ Object

Ticket 19135



141
142
143
144
# File 'app/models/platform/application.rb', line 141

def allow_grant_type_password!(value=true)
  allow_grant_type_password(value)
  save!
end

#allow_grant_type_password?Boolean

Ticket 19135

Returns:

  • (Boolean)


147
148
149
# File 'app/models/platform/application.rb', line 147

def allow_grant_type_password?
  has_permission?(:grant_type_password)
end

#allow_unlimited_models(value = true) ⇒ Object

Ticket 19180



156
157
158
# File 'app/models/platform/application.rb', line 156

def allow_unlimited_models(value=true)
  set_permission(:unlimited_models, value)
end

#allow_unlimited_models!(value = true) ⇒ Object

Ticket 19180



161
162
163
164
# File 'app/models/platform/application.rb', line 161

def allow_unlimited_models!(value=true)
  allow_unlimited_models(value)
  save!
end

#allow_unlimited_models?Boolean

Ticket 19180

Returns:

  • (Boolean)


167
168
169
# File 'app/models/platform/application.rb', line 167

def allow_unlimited_models?
  has_permission?(:unlimited_models)
end

#app_urlObject



257
258
259
260
# File 'app/models/platform/application.rb', line 257

def app_url
  return url if canvas_name.blank?      
  "http://#{Platform::Config.site_base_url}/platform/apps/#{canvas_name}"
end

#authorize_user(user = Platform::Config.current_user) ⇒ Object



289
290
291
# File 'app/models/platform/application.rb', line 289

def authorize_user(user = Platform::Config.current_user)
  Platform::ApplicationUser.touch(self, user)
end

#authorized_user?(user = Platform::Config.current_user) ⇒ Boolean

Returns:

  • (Boolean)


293
294
295
# File 'app/models/platform/application.rb', line 293

def authorized_user?(user = Platform::Config.current_user)
  not Platform::ApplicationUser.find(:first, :conditions => ["application_id = ? and user_id = ?", self.id, user.id]).nil?
end

#category_namesObject

Category Management Methods



333
334
335
# File 'app/models/platform/application.rb', line 333

def category_names
  categories.collect{|cat| cat.name}.join(", ")
end

#create_access_token(params = {}) ⇒ Object



192
193
194
195
196
# File 'app/models/platform/application.rb', line 192

def create_access_token(params={})
  access_token = Platform::Oauth::AccessToken.create(params.merge(:application => self))
  Platform::ApplicationUser.touch(self, access_token.user)
  access_token
end

#create_client_token(params = {}) ⇒ Object



202
203
204
# File 'app/models/platform/application.rb', line 202

def create_client_token(params={})
  Platform::Oauth::ClientToken.create(params.merge(:application => self))
end

#create_refresh_token(params = {}) ⇒ Object



198
199
200
# File 'app/models/platform/application.rb', line 198

def create_refresh_token(params={})
  Platform::Oauth::RefreshToken.create(params.merge(:application => self))
end

#create_request_token(params = {}) ⇒ Object

If your application requires passing in extra parameters handle it here



188
189
190
# File 'app/models/platform/application.rb', line 188

def create_request_token(params={})
  Platform::Oauth::RequestToken.create(params.merge(:application => self))
end

#deauthorize_user(user = Platform::Config.current_user) ⇒ Object



297
298
299
300
301
302
303
304
305
# File 'app/models/platform/application.rb', line 297

def deauthorize_user(user = Platform::Config.current_user)
  valid_tokens_for_user(user).each do |token|
    token.invalidate!
  end
  app_user = Platform::ApplicationUser.for(self, user)
  app_user.destroy if app_user
  
  # ping the deauthorization url - maybe that should be done in a task
end

#developed_by?(dev = Platform::Config.current_developer) ⇒ Boolean

Returns:

  • (Boolean)


273
274
275
# File 'app/models/platform/application.rb', line 273

def developed_by?(dev = Platform::Config.current_developer)
  self.developer == dev
end

#icon_urlObject



214
215
216
217
218
219
220
221
# File 'app/models/platform/application.rb', line 214

def icon_url
  return Platform::Config.default_app_icon unless icon
  if Platform::Config.site_media_enabled?
    Platform::Config.icon_url(icon)  
  else
    icon.url
  end
end

#last_monthly_metricObject



312
313
314
# File 'app/models/platform/application.rb', line 312

def last_monthly_metric
  @last_monthly_metric ||= Platform::MonthlyApplicationMetric.find(:first, :conditions => ["application_id = ?", id], :order => "interval desc")
end

#last_token_for_user(user) ⇒ Object



114
115
116
# File 'app/models/platform/application.rb', line 114

def last_token_for_user(user)
  Platform::Oauth::OauthToken.find(:first, :conditions => ["application_id = ? and user_id = ?", self.id, user.id], :order => "updated_at desc")
end

#last_total_metricObject



316
317
318
# File 'app/models/platform/application.rb', line 316

def last_total_metric
  @last_total_metric ||= Platform::TotalApplicationMetric.find(:first, :conditions => ["application_id = ?", id], :order => "interval desc")
end

#logo_urlObject



233
234
235
236
237
238
239
240
# File 'app/models/platform/application.rb', line 233

def logo_url
  return Platform::Config. unless 
  if Platform::Config.site_media_enabled?
    Platform::Config.logo_url()  
  else
    .url
  end
end

#rate_limited(value = true) ⇒ Object



122
123
124
# File 'app/models/platform/application.rb', line 122

def rate_limited(value=true)
  set_permission(:no_rate_limit, !value)
end

#rate_limited!(value = true) ⇒ Object



126
127
128
129
# File 'app/models/platform/application.rb', line 126

def rate_limited!(value=true)
  rate_limited(value)
  save!
end

#rate_limited?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/platform/application.rb', line 131

def rate_limited?
  ! has_permission?(:no_rate_limit)
end

#rating_countObject



277
278
279
# File 'app/models/platform/application.rb', line 277

def rating_count
  @rating_count ||= Platform::Rating.count(:id, :conditions => ["object_type = ? and object_id = ?", self.class.name, self.id])
end

#rating_sumObject



281
282
283
# File 'app/models/platform/application.rb', line 281

def rating_sum
  @rating_sum ||= Platform::Rating.sum(:value, :conditions => ["object_type = ? and object_id = ?", self.class.name, self.id])
end

#recently_updated_discussionsObject



324
325
326
# File 'app/models/platform/application.rb', line 324

def recently_updated_discussions
  @recently_updated_discussions ||= Platform::ForumTopic.find(:all, :conditions => ["subject_type = ? and subject_id = ?", 'Platform::Application', self.id], :order => "updated_at desc", :limit => 5)    
end

#recently_updated_reviewsObject



320
321
322
# File 'app/models/platform/application.rb', line 320

def recently_updated_reviews
  @recently_updated_reviews ||= Platform::Rating.find(:all, :conditions => ["object_type = ? and object_id = ?", 'Platform::Application', self.id], :order => "updated_at desc", :limit => 5)    
end

#remove_categories(categories) ⇒ Object



358
359
360
361
362
# File 'app/models/platform/application.rb', line 358

def remove_categories(categories)
  categories.each do |cat|
    remove_category(cat)
  end
end

#remove_category(cat) ⇒ Object



350
351
352
353
354
355
356
# File 'app/models/platform/application.rb', line 350

def remove_category(cat)
  cat = Platform::Category.find_by_keyword(cat) if cat.is_a?(String)
  return nil if not cat
  
  app_cat = Platform::ApplicationCategory.find_by_application_id_and_category_id(self.id, cat.id)
  app_cat.destroy if app_cat                                                
end

#requires_signature?Boolean

grant type password apps should require signature

Returns:

  • (Boolean)


308
309
310
# File 'app/models/platform/application.rb', line 308

def requires_signature?
  false      
end

#reset_secret!Object



285
286
287
# File 'app/models/platform/application.rb', line 285

def reset_secret!
  update_attributes(:secret => Platform::Helper.generate_key(40)[0,40])
end

#short_descriptionObject



252
253
254
255
# File 'app/models/platform/application.rb', line 252

def short_description
  return description if description.blank? or description.length < 400
  "#{description[0..400]}..."
end

#short_nameObject



262
263
264
265
# File 'app/models/platform/application.rb', line 262

def short_name
  return name if name.length < 15
  "#{name[0..15]}..."
end

#store_icon(file) ⇒ Object



223
224
225
226
227
228
229
230
231
# File 'app/models/platform/application.rb', line 223

def store_icon(file)
  if Platform::Config.site_media_enabled?
    update_attributes(:icon => Platform::Config.create_media(file))
  else
    self.icon = Platform::Media::Image.create
    self.icon.write(file, :size => 16)
    self.save
  end  
end

#store_logo(file) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'app/models/platform/application.rb', line 242

def (file)
  if Platform::Config.site_media_enabled?
    update_attributes(:logo => Platform::Config.create_media(file))
  else  
    self. = Platform::Media::Image.create
    self..write(file, :size => 75)
    self.save
  end  
end

#update_rank!Object



267
268
269
270
271
# File 'app/models/platform/application.rb', line 267

def update_rank!
  total_rank = (rating_count == 0) ? 0 : (rating_sum/rating_count)
  self.update_attributes(:rank => total_rank)
  total_rank
end

#valid_tokens_for_user(user) ⇒ Object



118
119
120
# File 'app/models/platform/application.rb', line 118

def valid_tokens_for_user(user)
  Platform::Oauth::OauthToken.find(:all, :conditions => ["application_id = ? and user_id = ? and invalidated_at is null", self.id, user.id], :order => "created_at desc")
end

#versioned_nameObject



372
373
374
375
376
# File 'app/models/platform/application.rb', line 372

def versioned_name
  @versioned_name ||= begin
    "#{name} #{version}"
  end
end