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.



62
63
64
# File 'app/models/platform/application.rb', line 62

def token_callback_url
  @token_callback_url
end

Class Method Details



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

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



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

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



102
103
104
105
# File 'app/models/platform/application.rb', line 102

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



212
213
214
# File 'app/models/platform/application.rb', line 212

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



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

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



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

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

#add_category(cat) ⇒ Object



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

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


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

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

#allow_add_without_premium(value = true) ⇒ Object

Ticket 19507



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

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

#allow_add_without_premium!(value = true) ⇒ Object

Ticket 19507



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

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

#allow_add_without_premium?Boolean

Ticket 19507

Returns:

  • (Boolean)


185
186
187
# File 'app/models/platform/application.rb', line 185

def allow_add_without_premium?
  has_permission?(:add_without_premium)
end

#allow_grant_type_client_credentials?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'app/models/platform/application.rb', line 153

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



138
139
140
# File 'app/models/platform/application.rb', line 138

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

#allow_grant_type_password!(value = true) ⇒ Object

Ticket 19135



143
144
145
146
# File 'app/models/platform/application.rb', line 143

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

#allow_grant_type_password?Boolean

Ticket 19135

Returns:

  • (Boolean)


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

def allow_grant_type_password?
  has_permission?(:grant_type_password)
end

#allow_unlimited_models(value = true) ⇒ Object

Ticket 19180



158
159
160
# File 'app/models/platform/application.rb', line 158

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

#allow_unlimited_models!(value = true) ⇒ Object

Ticket 19180



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

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

#allow_unlimited_models?Boolean

Ticket 19180

Returns:

  • (Boolean)


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

def allow_unlimited_models?
  has_permission?(:unlimited_models)
end

#app_urlObject



259
260
261
262
# File 'app/models/platform/application.rb', line 259

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



291
292
293
# File 'app/models/platform/application.rb', line 291

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

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

Returns:

  • (Boolean)


295
296
297
# File 'app/models/platform/application.rb', line 295

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



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

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

#create_access_token(params = {}) ⇒ Object



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

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



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

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

#create_refresh_token(params = {}) ⇒ Object



200
201
202
# File 'app/models/platform/application.rb', line 200

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



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

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

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



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

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)


275
276
277
# File 'app/models/platform/application.rb', line 275

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

#icon_urlObject



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

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



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

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



116
117
118
# File 'app/models/platform/application.rb', line 116

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



318
319
320
# File 'app/models/platform/application.rb', line 318

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

#logo_urlObject



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

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



124
125
126
# File 'app/models/platform/application.rb', line 124

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

#rate_limited!(value = true) ⇒ Object



128
129
130
131
# File 'app/models/platform/application.rb', line 128

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

#rate_limited?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/models/platform/application.rb', line 133

def rate_limited?
  ! has_permission?(:no_rate_limit)
end

#rating_countObject



279
280
281
# File 'app/models/platform/application.rb', line 279

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

#rating_sumObject



283
284
285
# File 'app/models/platform/application.rb', line 283

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

#recently_updated_discussionsObject



326
327
328
# File 'app/models/platform/application.rb', line 326

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



322
323
324
# File 'app/models/platform/application.rb', line 322

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



360
361
362
363
364
# File 'app/models/platform/application.rb', line 360

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

#remove_category(cat) ⇒ Object



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

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)


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

def requires_signature?
  false      
end

#reset_secret!Object



287
288
289
# File 'app/models/platform/application.rb', line 287

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

#short_descriptionObject



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

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

#short_nameObject



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

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

#store_icon(file) ⇒ Object



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

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



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

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



269
270
271
272
273
# File 'app/models/platform/application.rb', line 269

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



120
121
122
# File 'app/models/platform/application.rb', line 120

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



374
375
376
377
378
# File 'app/models/platform/application.rb', line 374

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