diff options
author | Heiko Bernloehr <Heiko.Bernloehr@FreeIT.de> | 2017-03-07 23:28:49 +0100 |
---|---|---|
committer | Heiko Bernloehr <Heiko.Bernloehr@FreeIT.de> | 2017-03-07 23:28:49 +0100 |
commit | d76b724cf8bd1d3a03a28e73aa77381bae86cde9 (patch) | |
tree | 873fec79cdf71caa158c45218ffb643773f89ca2 | |
parent | c813a05329ad7cf5b5f187bc754a521dad6e2fb2 (diff) | |
download | ecs-d76b724cf8bd1d3a03a28e73aa77381bae86cde9.tar.gz ecs-d76b724cf8bd1d3a03a28e73aa77381bae86cde9.zip |
Changed queries in model scopes to Arel syntax.
-rw-r--r-- | app/models/community.rb | 13 | ||||
-rw-r--r-- | app/models/membership.rb | 30 | ||||
-rw-r--r-- | app/models/message.rb | 39 | ||||
-rw-r--r-- | app/models/participant.rb | 26 | ||||
-rw-r--r-- | app/models/ressource.rb | 2 |
5 files changed, 54 insertions, 56 deletions
diff --git a/app/models/community.rb b/app/models/community.rb index 19a4945..3137f09 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -24,13 +24,12 @@ class Community < ActiveRecord::Base validates_presence_of :name validates_uniqueness_of :name - scope :for_participant, lambda { |participant| { - :joins => [:memberships => :participant], - :conditions => { :participants => { :id => participant.id }}}} - - scope :for_message, lambda { |message| { - :joins => [:memberships => {:membership_messages => :message}], - :conditions => { :messages => { :id => message.id }}}} + scope :for_participant, lambda { |participant| + joins(:memberships => :participant). + where(:participants => { :id => participant.id })} + scope :for_message, lambda { |message| + joins(:memberships => {:membership_messages => :message}). + where(:messages => { :id => message.id })} end diff --git a/app/models/membership.rb b/app/models/membership.rb index 0da7af5..6c0b4d3 100644 --- a/app/models/membership.rb +++ b/app/models/membership.rb @@ -20,9 +20,9 @@ class Membership < ActiveRecord::Base belongs_to :participant belongs_to :community belongs_to :community_with_reduced_attributes, + -> { select("name, description, id") }, :class_name => "Community", - :foreign_key => "community_id", - :select => "name, description, id" + :foreign_key => "community_id" has_many :messages, :through => :membership_messages has_many :membership_messages, :dependent => :destroy @@ -30,22 +30,22 @@ class Membership < ActiveRecord::Base # returns memberships of the relation between a participant and a message # if no relationship then returns empty array. - scope :receiver, lambda { |participant_id,message_id| { - :joins => [:participant, {:membership_messages => :message}], - :conditions => { :participants => { :id => participant_id }, :messages => { :id => message_id } } } } + scope :receiver, lambda { |participant_id,message_id| + joins(:participant, :membership_messages => :message). + where(:participants => { :id => participant_id }, :messages => { :id => message_id })} - scope :receivers, lambda { |message_id| { - :joins => [:membership_messages => :message], - :select => :memberships.to_s+".id" + ", community_id, participant_id", - :conditions => { :messages => { :id => message_id } } } } + scope :receivers, lambda { |message_id| + joins(:membership_messages => :message). + select(:memberships.to_s+".id" + ", community_id, participant_id"). + where(:messages => { :id => message_id })} - scope :for_participant_id, lambda { |participant_id| { - :joins => [:participant], - :conditions => { :participants => { :id => participant_id } } } } + scope :for_participant_id, lambda { |participant_id| + joins(:participant). + where(:participants => { :id => participant_id })} - scope :for_participant_id_and_community_id, lambda { |participant_id,community_id| { - :joins => [:participant, :community], - :conditions => { :participants => { :id => participant_id }, :communities => { :id => community_id } } } } + scope :for_participant_id_and_community_id, lambda { |participant_id,community_id| + joins(:participant, :community). + where(:participants => { :id => participant_id }, :communities => { :id => community_id })} def self.senders(participant, message) sender_mids=[] diff --git a/app/models/message.rb b/app/models/message.rb index 6e50704..ed74835 100644 --- a/app/models/message.rb +++ b/app/models/message.rb @@ -19,7 +19,6 @@ class Message < ActiveRecord::Base require 'exceptions' - require 'json/add/rails' has_many :memberships, :through => :membership_messages has_many :membership_messages @@ -29,29 +28,29 @@ class Message < ActiveRecord::Base has_one :auth, :dependent => :destroy belongs_to :ressource - scope :for_participant_receiver, lambda {|participant| { - :joins => {:membership_messages => {:membership => :participant}}, - -> { order 'id ASC' }, - :conditions => {:participants => {:id => participant.id}}, - :readonly => false}} + scope :for_participant_receiver, lambda {|participant| + joins(:membership_messages => {:membership => :participant}). + order('id ASC'). + where(:participants => {:id => participant.id}). + readonly(false) } - scope :for_participant_sender, lambda {|participant| { - -> { order 'id ASC' }, - :conditions => {:sender => participant.id}, - :readonly => false}} + scope :for_participant_sender, lambda {|participant| + order('id ASC'). + where(:sender => participant.id). + readonly(false) } - scope :for_not_removed, lambda { { - -> { order 'id ASC' }, - :conditions => {:removed => false}}} + scope :for_not_removed, lambda { + order('id ASC'). + where(:removed => false) } - scope :for_removed, lambda { { - -> { order 'id ASC' }, - :conditions => {:removed => true}}} + scope :for_removed, lambda { + order('id ASC'). + where(:removed => true) } - scope :for_resource, lambda {|namespace, name| { - :joins => :ressource, - -> { order 'id ASC' }, - :conditions => {:ressources => {:namespace => namespace, :ressource => name}}}} + scope :for_resource, lambda {|namespace, name| + joins(:ressource). + order('id ASC'). + where(:ressources => {:namespace => namespace, :ressource => name})} def self.create__(request, app_namespace, ressource_name, participant) transaction do diff --git a/app/models/participant.rb b/app/models/participant.rb index a1a00ef..4fad70b 100644 --- a/app/models/participant.rb +++ b/app/models/participant.rb @@ -42,19 +42,19 @@ class Participant < ActiveRecord::Base accepts_nested_attributes_for :communities, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } accepts_nested_attributes_for :subparticipant, :allow_destroy => true - scope :order_id_asc, :order => "participants.id ASC" - scope :without_anonymous, :conditions => { :participants => { :anonymous => false } } - scope :anonymous, :conditions => { :participants => { :anonymous => true } } - scope :for_message, lambda { |message| { - :joins => [:memberships => {:membership_messages => :message}], - :conditions => {:messages => {:id => message.id}}}} - scope :for_community, lambda { |community| { - :joins => [:memberships => :community], - :conditions => { :communities => { :id => community.id }}}} - scope :itsyou, lambda { |itsyoupid| { :conditions => { :participants => { :id => itsyoupid } } } } - scope :only_subparticipants, :conditions => { :participants => { :ptype => TYPE[:sub] } } - scope :only_mainparticipants, :conditions => { :participants => { :ptype => TYPE[:main] } } - scope :only_anonymous, :conditions => { :participants => { :ptype => TYPE[:anonym] } } + scope :order_id_asc, -> { order("id ASC") } + scope :without_anonymous, -> { where(:anonymous => false) } + scope :anonymous, -> { where(:anonymous => true) } + scope :for_message, lambda { |message| + joins(:memberships => {:membership_messages => :message}). + where("messages.id = ?", message.id) } + scope :for_community, lambda { |community| + joins(:memberships => :community). + where("communities.id = ?", community.id) } + scope :itsyou, lambda { |itsyoupid| where(:id => itsyoupid) } + scope :only_subparticipants, -> { where(:ptype => TYPE[:sub]) } + scope :only_mainparticipants, -> { where(:ptype => TYPE[:main]) } + scope :only_anonymous, -> { where(:ptype => TYPE[:anonym]) } def self.reduced_attributes find :all, :select => "participants.id, participants.name, participants.description, participants.email, participants.dns, participants.organization_id, participants.ptype" diff --git a/app/models/ressource.rb b/app/models/ressource.rb index 3419b42..824d00a 100644 --- a/app/models/ressource.rb +++ b/app/models/ressource.rb @@ -22,7 +22,7 @@ class Ressource < ActiveRecord::Base after_save :rebuild_routes after_destroy :rebuild_routes - scope :list, :order => "namespace, ressource ASC" + scope :list, -> { order("namespace, ressource ASC") } def self.validates_ressource_path(namespace, ressource) r = Ressource.find_by_namespace_and_ressource(namespace, ressource) |