Class: Platform::Application
- 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
-
#token_callback_url ⇒ Object
Returns the value of attribute token_callback_url.
Class Method Summary collapse
- .featured_for_category(category, page = 1, per_page = 20) ⇒ Object
- .find_token(token_key) ⇒ Object
- .for(client_id) ⇒ Object
- .permissions ⇒ Object
- .regular_for_category(category, page = 1, per_page = 20) ⇒ Object
Instance Method Summary collapse
- #add_categories(catigories) ⇒ Object
- #add_category(cat) ⇒ Object
- #admin_link ⇒ Object
-
#allow_add_without_premium(value = true) ⇒ Object
Ticket 19507.
-
#allow_add_without_premium!(value = true) ⇒ Object
Ticket 19507.
-
#allow_add_without_premium? ⇒ Boolean
Ticket 19507.
- #allow_grant_type_client_credentials? ⇒ Boolean
-
#allow_grant_type_password(value = true) ⇒ Object
Ticket 19135.
-
#allow_grant_type_password!(value = true) ⇒ Object
Ticket 19135.
-
#allow_grant_type_password? ⇒ Boolean
Ticket 19135.
-
#allow_unlimited_models(value = true) ⇒ Object
Ticket 19180.
-
#allow_unlimited_models!(value = true) ⇒ Object
Ticket 19180.
-
#allow_unlimited_models? ⇒ Boolean
Ticket 19180.
- #app_url ⇒ Object
- #authorize_user(user = Platform::Config.current_user) ⇒ Object
- #authorized_user?(user = Platform::Config.current_user) ⇒ Boolean
-
#category_names ⇒ Object
Category Management Methods.
- #create_access_token(params = {}) ⇒ Object
- #create_client_token(params = {}) ⇒ Object
- #create_refresh_token(params = {}) ⇒ Object
-
#create_request_token(params = {}) ⇒ Object
If your application requires passing in extra parameters handle it here.
- #deauthorize_user(user = Platform::Config.current_user) ⇒ Object
- #developed_by?(dev = Platform::Config.current_developer) ⇒ Boolean
- #icon_url ⇒ Object
- #last_monthly_metric ⇒ Object
- #last_token_for_user(user) ⇒ Object
- #last_total_metric ⇒ Object
- #logo_url ⇒ Object
- #oauth_url ⇒ Object
- #rate_limited(value = true) ⇒ Object
- #rate_limited!(value = true) ⇒ Object
- #rate_limited? ⇒ Boolean
- #rating_count ⇒ Object
- #rating_sum ⇒ Object
- #recently_updated_discussions ⇒ Object
- #recently_updated_reviews ⇒ Object
- #remove_categories(categories) ⇒ Object
- #remove_category(cat) ⇒ Object
-
#requires_signature? ⇒ Boolean
grant type password apps should require signature.
- #reset_secret! ⇒ Object
- #short_description ⇒ Object
- #short_name ⇒ Object
- #store_icon(file) ⇒ Object
- #store_logo(file) ⇒ Object
- #update_rank! ⇒ Object
- #valid_tokens_for_user(user) ⇒ Object
- #versioned_name ⇒ Object
Methods included from SimpleStringPermissions
#has_permission?, #set_permission
Instance Attribute Details
#token_callback_url ⇒ Object
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
.featured_for_category(category, page = 1, per_page = 20) ⇒ Object
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. 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 |
.permissions ⇒ Object
257 258 259 |
# File 'app/models/platform/application.rb', line 257 def self. [: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 |
#admin_link ⇒ Object
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) (: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
230 231 232 |
# File 'app/models/platform/application.rb', line 230 def allow_add_without_premium? (:add_without_premium) end |
#allow_grant_type_client_credentials? ⇒ 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) (: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
194 195 196 |
# File 'app/models/platform/application.rb', line 194 def allow_grant_type_password? (: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) (: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
214 215 216 |
# File 'app/models/platform/application.rb', line 214 def allow_unlimited_models? (:unlimited_models) end |
#app_url ⇒ Object
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 (user = Platform::Config.current_user) Platform::ApplicationUser.touch(self, user) end |
#authorized_user?(user = Platform::Config.current_user) ⇒ Boolean
340 341 342 |
# File 'app/models/platform/application.rb', line 340 def (user = Platform::Config.current_user) not Platform::ApplicationUser.find(:first, :conditions => ["application_id = ? and user_id = ?", self.id, user.id]).nil? end |
#category_names ⇒ Object
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 (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
320 321 322 |
# File 'app/models/platform/application.rb', line 320 def developed_by?(dev = Platform::Config.current_developer) self.developer == dev end |
#icon_url ⇒ Object
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_metric ⇒ Object
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_metric ⇒ Object
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_url ⇒ Object
280 281 282 283 284 285 286 287 |
# File 'app/models/platform/application.rb', line 280 def logo_url return Platform::Config.default_app_logo unless logo if Platform::Config.site_media_enabled? Platform::Config.logo_url(logo) else logo.url end end |
#oauth_url ⇒ Object
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) (: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
178 179 180 |
# File 'app/models/platform/application.rb', line 178 def rate_limited? ! (:no_rate_limit) end |
#rating_count ⇒ Object
324 325 326 |
# File 'app/models/platform/application.rb', line 324 def @rating_count ||= Platform::Rating.count(:id, :conditions => ["object_type = ? and object_id = ?", self.class.name, self.id]) end |
#rating_sum ⇒ Object
328 329 330 |
# File 'app/models/platform/application.rb', line 328 def @rating_sum ||= Platform::Rating.sum(:value, :conditions => ["object_type = ? and object_id = ?", self.class.name, self.id]) end |
#recently_updated_discussions ⇒ Object
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_reviews ⇒ Object
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
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_description ⇒ Object
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_name ⇒ Object
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 store_logo(file) if Platform::Config.site_media_enabled? update_attributes(:logo => Platform::Config.create_media(file)) else self.logo = Platform::Media::Image.create self.logo.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 = ( == 0) ? 0 : (/) 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_name ⇒ Object
419 420 421 422 423 |
# File 'app/models/platform/application.rb', line 419 def versioned_name @versioned_name ||= begin "#{name} #{version}" end end |