Class: Platform::Application

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

Overview

– Copyright © 2010-2012 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. ++

– Platform::Application Schema Information

Table name: platform_applications

id                          INTEGER         not null, primary key
developer_id                integer         
name                        varchar(255)    
description                 text            
state                       varchar(255)    default = new
locale                      varchar(255)    
url                         varchar(255)    
site_domain                 varchar(255)    
support_url                 varchar(255)    
callback_url                varchar(255)    
contact_email               varchar(255)    
privacy_policy_url          varchar(255)    
terms_of_service_url        varchar(255)    
permissions                 varchar(255)    
key                         varchar(255)    
secret                      varchar(255)    
icon_id                     integer         
logo_id                     integer         
canvas_name                 varchar(255)    
canvas_url                  varchar(255)    
auto_resize                 boolean         
auto_login                  boolean         
mobile_application_type     varchar(255)    
ios_bundle_id               varchar(255)    
itunes_app_store_id         varchar(255)    
android_key_hash            varchar(255)    
rank                        integer         
auto_signin                 boolean         
deauthorize_callback_url    varchar(255)    
version                     varchar(255)    
api_version                 varchar(255)    
parent_id                   integer         
created_at                  datetime        
updated_at                  datetime

Indexes

index_platform_applications_on_parent_id       (parent_id) 
index_platform_applications_on_key             (key) UNIQUE
index_platform_applications_on_developer_id    (developer_id)

++

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.



107
108
109
# File 'app/models/platform/application.rb', line 107

def token_callback_url
  @token_callback_url
end

Class Method Details



411
412
413
# File 'app/models/platform/application.rb', line 411

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



152
153
154
155
156
157
158
159
# File 'app/models/platform/application.rb', line 152

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



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

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



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

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



415
416
417
# File 'app/models/platform/application.rb', line 415

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



391
392
393
394
395
# File 'app/models/platform/application.rb', line 391

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

#add_category(cat) ⇒ Object



384
385
386
387
388
389
# File 'app/models/platform/application.rb', line 384

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


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

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

#allow_add_without_premium(value = true) ⇒ Object

Ticket 19507



219
220
221
# File 'app/models/platform/application.rb', line 219

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

#allow_add_without_premium!(value = true) ⇒ Object

Ticket 19507



224
225
226
227
# File 'app/models/platform/application.rb', line 224

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

#allow_add_without_premium?Boolean

Ticket 19507

Returns:

  • (Boolean)


230
231
232
# File 'app/models/platform/application.rb', line 230

def allow_add_without_premium?
  has_permission?(:add_without_premium)
end

#allow_grant_type_client_credentials?Boolean

Returns:

  • (Boolean)


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

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



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

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

#allow_grant_type_password!(value = true) ⇒ Object

Ticket 19135



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

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

#allow_grant_type_password?Boolean

Ticket 19135

Returns:

  • (Boolean)


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

def allow_grant_type_password?
  has_permission?(:grant_type_password)
end

#allow_unlimited_models(value = true) ⇒ Object

Ticket 19180



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

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

#allow_unlimited_models!(value = true) ⇒ Object

Ticket 19180



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

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

#allow_unlimited_models?Boolean

Ticket 19180

Returns:

  • (Boolean)


214
215
216
# File 'app/models/platform/application.rb', line 214

def allow_unlimited_models?
  has_permission?(:unlimited_models)
end

#app_urlObject



304
305
306
307
# File 'app/models/platform/application.rb', line 304

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



336
337
338
# File 'app/models/platform/application.rb', line 336

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

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

Returns:

  • (Boolean)


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

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



380
381
382
# File 'app/models/platform/application.rb', line 380

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

#create_access_token(params = {}) ⇒ Object



239
240
241
242
243
# File 'app/models/platform/application.rb', line 239

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



249
250
251
# File 'app/models/platform/application.rb', line 249

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

#create_refresh_token(params = {}) ⇒ Object



245
246
247
# File 'app/models/platform/application.rb', line 245

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



235
236
237
# File 'app/models/platform/application.rb', line 235

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

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



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

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)


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

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

#icon_urlObject



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

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



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

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



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

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



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

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

#logo_urlObject



280
281
282
283
284
285
286
287
# File 'app/models/platform/application.rb', line 280

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

#oauth_urlObject



425
426
427
428
# File 'app/models/platform/application.rb', line 425

def oauth_url
  protocol = Platform::Config.env == "development" ? 'http' : 'https'
	"#{protocol}://#{Platform::Config.site_base_url}/platform/oauth/authorize?client_id=#{key}&response_type=token&display=web&redirect_url=#{CGI.escape(callback_url || '')}"
end

#rate_limited(value = true) ⇒ Object



169
170
171
# File 'app/models/platform/application.rb', line 169

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

#rate_limited!(value = true) ⇒ Object



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

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

#rate_limited?Boolean

Returns:

  • (Boolean)


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

def rate_limited?
  ! has_permission?(:no_rate_limit)
end

#rating_countObject



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

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

#rating_sumObject



328
329
330
# File 'app/models/platform/application.rb', line 328

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

#recently_updated_discussionsObject



371
372
373
# File 'app/models/platform/application.rb', line 371

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



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

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



405
406
407
408
409
# File 'app/models/platform/application.rb', line 405

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

#remove_category(cat) ⇒ Object



397
398
399
400
401
402
403
# File 'app/models/platform/application.rb', line 397

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)


355
356
357
# File 'app/models/platform/application.rb', line 355

def requires_signature?
  false      
end

#reset_secret!Object



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

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

#short_descriptionObject



299
300
301
302
# File 'app/models/platform/application.rb', line 299

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

#short_nameObject



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

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

#store_icon(file) ⇒ Object



270
271
272
273
274
275
276
277
278
# File 'app/models/platform/application.rb', line 270

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



289
290
291
292
293
294
295
296
297
# File 'app/models/platform/application.rb', line 289

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



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

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



165
166
167
# File 'app/models/platform/application.rb', line 165

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



419
420
421
422
423
# File 'app/models/platform/application.rb', line 419

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