Skip to content

Commit

Permalink
Added check to make sure the # of rooms is respected when adding more…
Browse files Browse the repository at this point in the history
… rooms
  • Loading branch information
gabceb committed Feb 27, 2013
1 parent b0c0669 commit 9d94081
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/assets/javascripts/backbone/views/channel_tabs.js.coffee
Expand Up @@ -33,8 +33,8 @@ class Kandan.Views.ChannelTabs extends Backbone.View
success: (model)->
Kandan.Helpers.Channels.createChannelArea(model)

error: (model)->
alert("Something went wrong while creating a new Room.\n\nMaybe the room name is already taken?")
error: (model, response)->
_.each(JSON.parse(response.responseText), alert);
})
console.log "create channel: #{channelName}"
return false
Expand Down
12 changes: 11 additions & 1 deletion app/models/channel.rb
Expand Up @@ -2,7 +2,17 @@ class Channel < ActiveRecord::Base
has_many :activities, :dependent => :destroy
has_many :attachments, :dependent => :destroy

validates :name, :presence => true, :uniqueness => true
validates :name, :presence => { :message => "Room name cannot be blank"}, :uniqueness => { :message => "Room name is already taken" }

before_create :ensure_app_max_rooms

def ensure_app_max_rooms
valid = Setting.my_settings.max_rooms > Channel.count

self.errors.add(:max_rooms, "This app has reached the maximum number of rooms") unless valid

valid
end

class << self
def user_connect(user)
Expand Down
2 changes: 1 addition & 1 deletion app/models/setting.rb
Expand Up @@ -28,7 +28,7 @@ def ensure_only_one_settings

# Making sure the max_rooms is an integer and is never less than the current number of rooms
def validate_max_rooms
self.values[:max_rooms].is_a?(Integer) && self.values[:max_rooms] >= Channel.count unless self.new_record?
self.values[:max_rooms].is_a?(Integer) && self.values[:max_rooms] > 0 && self.values[:max_rooms] >= Channel.count unless self.new_record?
end

# Making sure the public site is a boolean
Expand Down

0 comments on commit 9d94081

Please sign in to comment.