Skip to content

Commit

Permalink
Made organization create a default redhat provider on its inception
Browse files Browse the repository at this point in the history
  • Loading branch information
parthaa committed Sep 29, 2011
1 parent 644348d commit 193b434
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/app/models/organization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Organization < ActiveRecord::Base
include Authorization

has_many :activation_keys, :dependent => :destroy
has_many :providers
has_many :providers, :dependent => :destroy
has_many :environments, :class_name => "KTEnvironment", :conditions => {:locker => false}, :dependent => :destroy, :inverse_of => :organization
has_one :locker, :class_name =>"KTEnvironment", :conditions => {:locker => true}, :dependent => :destroy

Expand All @@ -33,6 +33,7 @@ class Organization < ActiveRecord::Base
scoped_search :in => :providers, :on => :repository_url, :complete_value => true, :rename => :'provider.url'

before_create :create_locker
before_create :create_redhat_provider
validates :name, :uniqueness => true, :presence => true, :katello_name_format => true
validates :description, :katello_description_format => true

Expand All @@ -48,10 +49,18 @@ def promotion_paths
end
end

def redhat_provider
self.providers.redhat.first
end

def create_locker
self.locker = KTEnvironment.new(:name => "Locker", :locker => true, :organization => self)
end

def create_redhat_provider
self.providers << ::Provider.new(:name => "Red Hat", :provider_type=> ::Provider::REDHAT, :organization => self)
end

#permissions
scope :readable, lambda {authorized_items(READ_PERM_VERBS)}

Expand Down
17 changes: 13 additions & 4 deletions src/app/models/provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ class Provider < ActiveRecord::Base
scoped_search :in => :products, :on => :description, :complete_value => true, :rename => :'custom_product.description'

validate :only_one_rhn_provider
validate :valid_url, :if => :rh_repo?

validate :valid_url, :if => :redhat_provider?

scope :redhat, where(:provider_type => REDHAT)
scope :custom, where(:provider_type => CUSTOM)
def only_one_rhn_provider
# validate only when new record is added (skip explicit valid? calls)
if new_record? and provider_type == REDHAT and count_providers(REDHAT) != 0
Expand All @@ -63,15 +64,19 @@ def yum_repo?
provider_type == CUSTOM
end

def rh_repo?
def redhat_provider=(is_rh)
provider_type = is_rh ? REDHAT : CUSTOM
end

def redhat_provider?
provider_type == REDHAT
end

# Logic to ask a Provider if it is one that has subscriptions managed for
# the products contained within. Right now this is just redhat products but
# wanted to centralize the logic in one method.
def has_subscriptions?
rh_repo?
redhat_provider?
end

#permissions
Expand Down Expand Up @@ -130,6 +135,10 @@ def serializable_hash(options={})
protected

def sanitize_repository_url
if redhat_provider? && self.repository_url.blank?
self.repository_url = AppConfig.REDHAT_REPOSITORY_URL
self.repository_url = "https://cdn.redhat.com" unless self.repository_url
end
if self.repository_url
self.repository_url.strip!
end
Expand Down
1 change: 1 addition & 0 deletions src/spec/models/organization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
context "create an organization" do
specify {@organization.name.should == 'test_organization'}
specify {@organization.locker.should_not be_nil}
specify {@organization.redhat_provider.should_not be_nil}
specify {@organization.environments.should be_empty}
specify {Organization.where(:name => @organization.name).size.should == 1}
specify {Organization.where(:name => @organization.name).first.should == @organization}
Expand Down

0 comments on commit 193b434

Please sign in to comment.