Improve channel, user and api_key models with basic scopes, correct references and protect attributes.

Simplify channels & user controllers and models, moving code to the models.
Remove catch-all route and ensure all actions are specified, fixing url paths.
Change clone to dup in feed_controller as using clone seems to affect the cloned object (problem only in ruby 1.9.3?)
This commit is contained in:
Alan Bradburne
2012-02-09 23:42:16 +00:00
parent bfd5b29a9a
commit 4f0354303f
21 changed files with 198 additions and 172 deletions

View File

@ -1,7 +1,22 @@
class ApiKey < ActiveRecord::Base
belongs_to :channel
belongs_to :channel
belongs_to :user
validates_uniqueness_of :api_key
validates_uniqueness_of :api_key
scope :write_keys, :conditions => { :write_flag => true }
scope :read_keys, :conditions => { :write_flag => false }
attr_readonly :created_at
attr_accessible :note
def to_s
api_key
end
def to_param
api_key
end
end

View File

@ -1,7 +1,40 @@
class Channel < ActiveRecord::Base
belongs_to :user
include KeyUtilities
belongs_to :user
has_many :feeds
has_many :api_keys
attr_readonly :created_at
attr_protected :user_id, :last_entry_id
after_create :set_initial_default_name
before_validation :set_default_name
validates :name, :presence => true, :on => :update
def add_write_api_key
write_key = self.api_keys.new
write_key.user = self.user
write_key.write_flag = true
write_key.api_key = generate_api_key
write_key.save
end
def field_label(field_number)
self["field#{field_number}"]
end
private
def set_default_name
self.name = "#{I18n.t(:channel_default_name)} #{self.id}" if self.name.blank?
end
def set_initial_default_name
update_attribute(:name, "#{I18n.t(:channel_default_name)} #{self.id}")
end
end

View File

@ -1,7 +1,10 @@
class Feed < ActiveRecord::Base
belongs_to :channel
self.include_root_in_json = false
self.include_root_in_json = false
attr_readonly :created_at
attr_protected :channel_id
end

View File

@ -1,11 +1,12 @@
class User < ActiveRecord::Base
has_many :channels
has_many :channels
has_many :api_keys
acts_as_authentic
acts_as_authentic
def self.find_by_login_or_email(login)
User.find_by_login(login) || User.find_by_email(login)
end
def self.find_by_login_or_email(login)
User.find_by_login(login) || User.find_by_email(login)
end
end