update plugins to allow listing via API

This commit is contained in:
Lee Lawlor
2014-10-17 14:51:58 -04:00
parent c527515d4a
commit 1b44dfbd4b
10 changed files with 336 additions and 10 deletions

View File

@ -18,6 +18,44 @@ class Plugin < ActiveRecord::Base
has_many :windows, -> { where window_type: 'plugin' }, :foreign_key => :content_id, :source => :window
before_destroy { |record| record.windows.each { |window| window.delete } }
# pagination variables
cattr_reader :per_page
@@per_page = 15
# paginated hash for json and xml output
# plugins input must be paginated
def self.paginated_hash(plugins)
{
pagination:
{
current_page: plugins.current_page,
per_page: plugins.per_page,
total_entries: plugins.total_entries,
},
plugins: plugins.as_json(Plugin.public_options)
}
end
# for to_json or to_xml, return only the public attributes
def self.public_options
{
:root => false,
:only => [:id, :name, :created_at, :public_flag],
:methods => [:username, :url]
}
end
# login name of the user who created the plugin
def username; self.user.try(:login); end
# url for the plugin
def url
domain = 'https://thingspeak.com'
domain = 'http://staging.thingspeak.com' if Rails.env.staging?
domain = 'http://127.0.0.1:3000' if Rails.env.development?
return "#{domain}/plugins/#{self.id}"
end
def destroy_window
window_id = Window.where(content_id: self.id, window_type: 'plugin').first.id
Window.delete(window_id)