On 08/13/2012 08:26 PM, Jozef Zigmund wrote:
On Thu, 2012-08-02 at 14:24 +0200, ifarkas@redhat.com wrote:
From: Imre Farkas ifarkas@redhat.com
src/app/models/pool.rb | 2 + src/app/models/provider.rb | 2 + src/app/models/provider_account.rb | 2 + src/app/models/provider_priority_group.rb | 50 +++++++++++++++++++++++ src/app/models/provider_priority_group_element.rb | 22 ++++++++++ 5 files changed, 78 insertions(+) create mode 100644 src/app/models/provider_priority_group.rb create mode 100644 src/app/models/provider_priority_group_element.rb
diff --git a/src/app/models/pool.rb b/src/app/models/pool.rb index 13baebc..23d31c6 100644 --- a/src/app/models/pool.rb +++ b/src/app/models/pool.rb @@ -66,6 +66,8 @@ class Pool < ActiveRecord::Base
has_many :deployments, :dependent => :destroy
- has_many :provider_priority_groups, :dependent => :destroy
^^ this could be :dependent => :delete_all as no callbacks need to be called for deleting priority groups associated with pool ??
Yeah, there is no callback *for now*. But in the future if some will be added, there will be a high chance that this would be forgotten and will resurrect in some form of BZ. :-) Also, the performance gain you can except from delete_all is just a few ms and I'm pretty sure that the user will never notice it. So, I think tolerance against future changes has much more benefit over gaining few ms in server response.
before_destroy :destroyable?
def cloud_accounts
diff --git a/src/app/models/provider.rb b/src/app/models/provider.rb index cf2a7e7..64ddb33 100644 --- a/src/app/models/provider.rb +++ b/src/app/models/provider.rb @@ -59,6 +59,8 @@ class Provider < ActiveRecord::Base has_many :derived_permissions, :as => :permission_object, :dependent => :destroy, :include => [:role], :order => "derived_permissions.id ASC"
has_many :provider_priority_group_elements, :as => :value, :dependent => :destroy
has_many :provider_priority_groups, :through => :provider_priority_group_elements
before_destroy :destroyable?
diff --git a/src/app/models/provider_account.rb b/src/app/models/provider_account.rb index 067f5cf..1e8d41c 100644 --- a/src/app/models/provider_account.rb +++ b/src/app/models/provider_account.rb @@ -58,6 +58,8 @@ class ProviderAccount < ActiveRecord::Base # eventually, this might be "has_many", but first pass is one-to-one has_one :config_server, :dependent => :destroy
- has_many :provider_priority_group_elements, :as => :value, :dependent => :destroy
- # Helpers attr_accessor :x509_cert_priv_file, :x509_cert_pub_file
diff --git a/src/app/models/provider_priority_group.rb b/src/app/models/provider_priority_group.rb new file mode 100644 index 0000000..1e1a594 --- /dev/null +++ b/src/app/models/provider_priority_group.rb @@ -0,0 +1,50 @@ +# +# Copyright 2012 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#
+class ProviderPriorityGroup < ActiveRecord::Base
- belongs_to :pool
- has_many :provider_priority_group_elements, :dependent => :destroy
- has_many :providers, :through => :provider_priority_group_elements, :source => :value, :source_type => 'Provider'
- has_many :provider_accounts, :through => :provider_priority_group_elements, :source => :value, :source_type => 'ProviderAccount'
- validates_numericality_of :score, :only_integer => true, :greater_than_or_equal_to => -100, :less_than_or_equal_to => 100
- def include?(element)
- if element.is_a?(Provider)
providers.include?(element)
- elsif element.is_a?(ProviderAccount)
providers.include?(element.provider) || provider_accounts.include?(element)
- end
- end
- def add_provider_accounts(selected_provider_accounts)
- selected_provider_accounts.each do |provider_account|
unless providers.include?(provider_account.provider)
provider_accounts << provider_account
end
- end
- end
- def all_provider_accounts
- result = providers.inject([]) do |result, provider|
result += provider.provider_accounts
- end
- result += provider_accounts
- end
+end \ No newline at end of file diff --git a/src/app/models/provider_priority_group_element.rb b/src/app/models/provider_priority_group_element.rb new file mode 100644 index 0000000..65d1c7b --- /dev/null +++ b/src/app/models/provider_priority_group_element.rb @@ -0,0 +1,22 @@ +# +# Copyright 2012 Red Hat, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +#
+class ProviderPriorityGroupElement < ActiveRecord::Base
- belongs_to :provider_priority_group
- belongs_to :value, :polymorphic => true
+end \ No newline at end of file