Browse Source

Some new rubocop checks.

Martin Edenhofer 10 years ago
parent
commit
bd29c71687

+ 24 - 23
app/models/application_model/activity_stream_base.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module ApplicationModel::ActivityStreamBase
+class ApplicationModel
+  module ActivityStreamBase
 
 =begin
 
@@ -18,28 +19,28 @@ returns
 
 =end
 
-  def activity_stream_log (type, user_id, force = false)
-
-    # return if we run import mode
-    return if Setting.get('import_mode')
-
-    # return if we run on init mode
-    return if !Setting.get('system_init_done')
-
-    role       = self.class.activity_stream_support_config[:role]
-    updated_at = self.updated_at
-    if force
-      updated_at = Time.new
+    def activity_stream_log (type, user_id, force = false)
+
+      # return if we run import mode
+      return if Setting.get('import_mode')
+
+      # return if we run on init mode
+      return if !Setting.get('system_init_done')
+
+      role       = self.class.activity_stream_support_config[:role]
+      updated_at = self.updated_at
+      if force
+        updated_at = Time.new
+      end
+      ActivityStream.add(
+        o_id: self['id'],
+        type: type,
+        object: self.class.name,
+        group_id: self['group_id'],
+        role: role,
+        created_at: updated_at,
+        created_by_id: user_id,
+      )
     end
-    ActivityStream.add(
-      o_id: self['id'],
-      type: type,
-      object: self.class.name,
-      group_id: self['group_id'],
-      role: role,
-      created_at: updated_at,
-      created_by_id: user_id,
-    )
   end
-
 end

+ 19 - 18
app/models/application_model/assets.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module ApplicationModel::Assets
+class ApplicationModel
+  module Assets
 
 =begin
 
@@ -20,24 +21,24 @@ returns
 
 =end
 
-  def assets (data = {})
+    def assets (data = {})
 
-    if !data[ self.class.to_app_model ]
-      data[ self.class.to_app_model ] = {}
-    end
-    if !data[ self.class.to_app_model ][ self.id ]
-      data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
-    end
-
-    return data if !self['created_by_id'] && !self['updated_by_id']
-    ['created_by_id', 'updated_by_id'].each {|item|
-      next if !self[ item ]
-      if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
-        user = User.lookup( id: self[ item ] )
-        data = user.assets( data )
+      if !data[ self.class.to_app_model ]
+        data[ self.class.to_app_model ] = {}
+      end
+      if !data[ self.class.to_app_model ][ self.id ]
+        data[ self.class.to_app_model ][ self.id ] = self.attributes_with_associations
       end
-    }
-    data
-  end
 
+      return data if !self['created_by_id'] && !self['updated_by_id']
+      ['created_by_id', 'updated_by_id'].each {|item|
+        next if !self[ item ]
+        if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self[ item ] ]
+          user = User.lookup( id: self[ item ] )
+          data = user.assets( data )
+        end
+      }
+      data
+    end
+  end
 end

+ 9 - 7
app/models/application_model/background_job_search_index.rb

@@ -1,11 +1,13 @@
 # perform background job
-class ApplicationModel::BackgroundJobSearchIndex
-  def initialize(object, o_id)
-    @object = object
-    @o_id   = o_id
-  end
+class ApplicationModel
+  class BackgroundJobSearchIndex
+    def initialize(object, o_id)
+      @object = object
+      @o_id   = o_id
+    end
 
-  def perform
-    Object.const_get(@object).find(@o_id).search_index_update_backend
+    def perform
+      Object.const_get(@object).find(@o_id).search_index_update_backend
+    end
   end
 end

+ 30 - 29
app/models/application_model/history_log_base.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module ApplicationModel::HistoryLogBase
+class ApplicationModel
+  module HistoryLogBase
 
 =begin
 
@@ -15,15 +16,15 @@ returns
 
 =end
 
-  def history_log (type, user_id, data = {})
-    data[:o_id]                   = self['id']
-    data[:history_type]           = type
-    data[:history_object]         = self.class.name
-    data[:related_o_id]           = nil
-    data[:related_history_object] = nil
-    data[:created_by_id]          = user_id
-    History.add(data)
-  end
+    def history_log (type, user_id, data = {})
+      data[:o_id]                   = self['id']
+      data[:history_type]           = type
+      data[:history_object]         = self.class.name
+      data[:related_o_id]           = nil
+      data[:related_history_object] = nil
+      data[:created_by_id]          = user_id
+      History.add(data)
+    end
 
 =begin
 
@@ -74,27 +75,27 @@ returns
 
 =end
 
-  def history_get(fulldata = false)
-    if !fulldata
-      return History.list( self.class.name, self['id'] )
-    end
-
-    # get related objects
-    history = History.list( self.class.name, self['id'], nil, true )
-    history[:list].each {|item|
-      record = Kernel.const_get( item['object'] ).find( item['o_id'] )
+    def history_get(fulldata = false)
+      if !fulldata
+        return History.list( self.class.name, self['id'] )
+      end
 
-      history[:assets] = record.assets( history[:assets] )
+      # get related objects
+      history = History.list( self.class.name, self['id'], nil, true )
+      history[:list].each {|item|
+        record = Kernel.const_get( item['object'] ).find( item['o_id'] )
 
-      if item['related_object']
-        record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
         history[:assets] = record.assets( history[:assets] )
-      end
-    }
-    return {
-      history: history[:list],
-      assets: history[:assets],
-    }
-  end
 
+        if item['related_object']
+          record = Kernel.const_get( item['related_object'] ).find( item['related_o_id'] )
+          history[:assets] = record.assets( history[:assets] )
+        end
+      }
+      {
+        history: history[:list],
+        assets: history[:assets],
+      }
+    end
+  end
 end

+ 89 - 88
app/models/application_model/search_index_base.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module ApplicationModel::SearchIndexBase
+class ApplicationModel
+  module SearchIndexBase
 
 =begin
 
@@ -15,47 +16,47 @@ returns
 
 =end
 
-  def search_index_update_backend
-    return if !self.class.search_index_support_config
-
-    # default ignored attributes
-    ignore_attributes = {
-      created_by_id: true,
-      updated_by_id: true,
-      active: true,
-    }
-    if self.class.search_index_support_config[:ignore_attributes]
-      self.class.search_index_support_config[:ignore_attributes].each {|key, value|
-        ignore_attributes[key] = value
-      }
-    end
+    def search_index_update_backend
+      return if !self.class.search_index_support_config
 
-    # for performance reasons, Model.search_index_reload will only collect if of object
-    # get whole data here
-    data = self.class.find(self.id)
+      # default ignored attributes
+      ignore_attributes = {
+        created_by_id: true,
+        updated_by_id: true,
+        active: true,
+      }
+      if self.class.search_index_support_config[:ignore_attributes]
+        self.class.search_index_support_config[:ignore_attributes].each {|key, value|
+          ignore_attributes[key] = value
+        }
+      end
 
-    # remove ignored attributes
-    attributes = data.attributes
-    ignore_attributes.each {|key, value|
-      next if value != true
-      attributes.delete( key.to_s )
-    }
+      # for performance reasons, Model.search_index_reload will only collect if of object
+      # get whole data here
+      data = self.class.find(self.id)
 
-    # fill up with search data
-    attributes = search_index_attribute_lookup(attributes, data)
-    return if !attributes
+      # remove ignored attributes
+      attributes = data.attributes
+      ignore_attributes.each {|key, value|
+        next if value != true
+        attributes.delete( key.to_s )
+      }
 
-    # update backend
-    if self.class.column_names.include? 'active'
-      if self.active
-        SearchIndexBackend.add( self.class.to_s, attributes )
+      # fill up with search data
+      attributes = search_index_attribute_lookup(attributes, data)
+      return if !attributes
+
+      # update backend
+      if self.class.column_names.include? 'active'
+        if self.active
+          SearchIndexBackend.add( self.class.to_s, attributes )
+        else
+          SearchIndexBackend.remove( self.class.to_s, self.id )
+        end
       else
-        SearchIndexBackend.remove( self.class.to_s, self.id )
+        SearchIndexBackend.add( self.class.to_s, attributes )
       end
-    else
-      SearchIndexBackend.add( self.class.to_s, attributes )
     end
-  end
 
 =begin
 
@@ -70,16 +71,16 @@ returns
 
 =end
 
-  def search_index_data
-    attributes = {}
-    ['name', 'note'].each { |key|
-      if self[key] && !self[key].empty?
-        attributes[key] = self[key]
-      end
-    }
-    return if attributes.empty?
-    attributes
-  end
+    def search_index_data
+      attributes = {}
+      ['name', 'note'].each { |key|
+        if self[key] && !self[key].empty?
+          attributes[key] = self[key]
+        end
+      }
+      return if attributes.empty?
+      attributes
+    end
 
   private
 
@@ -95,51 +96,51 @@ returns
 
 =end
 
-  def search_index_attribute_lookup(attributes, ref_object)
+    def search_index_attribute_lookup(attributes, ref_object)
 
-    # default keep attributes
-    keep_attributes = {}
-    if self.class.search_index_support_config[:keep_attributes]
-      self.class.search_index_support_config[:keep_attributes].each {|key, value|
-        keep_attributes[key] = value
-      }
-    end
-
-    attributes_new = {}
-    attributes.each {|key, value|
-      next if !value
-
-      # get attribute name
-      attribute_name_with_id = key.to_s
-      attribute_name         = key.to_s
-      next if attribute_name[-3, 3] != '_id'
-      attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
-
-      # check if attribute method exists
-      next if !ref_object.respond_to?( attribute_name )
-
-      # check if method has own class
-      relation_class = ref_object.send(attribute_name).class
-      next if !relation_class
-
-      # lookup ref object
-      relation_model = relation_class.lookup( id: value )
-      next if !relation_model
-
-      # get name of ref object
-      value = nil
-      if relation_model.respond_to?('search_index_data')
-        value = relation_model.send('search_index_data')
+      # default keep attributes
+      keep_attributes = {}
+      if self.class.search_index_support_config[:keep_attributes]
+        self.class.search_index_support_config[:keep_attributes].each {|key, value|
+          keep_attributes[key] = value
+        }
       end
-      next if !value
 
-      # save name of ref object
-      attributes_new[ attribute_name ] = value
-      if !keep_attributes[ attribute_name_with_id.to_sym ]
-        attributes.delete(key)
-      end
-    }
-    attributes_new.merge(attributes)
+      attributes_new = {}
+      attributes.each {|key, value|
+        next if !value
+
+        # get attribute name
+        attribute_name_with_id = key.to_s
+        attribute_name         = key.to_s
+        next if attribute_name[-3, 3] != '_id'
+        attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
+
+        # check if attribute method exists
+        next if !ref_object.respond_to?( attribute_name )
+
+        # check if method has own class
+        relation_class = ref_object.send(attribute_name).class
+        next if !relation_class
+
+        # lookup ref object
+        relation_model = relation_class.lookup( id: value )
+        next if !relation_model
+
+        # get name of ref object
+        value = nil
+        if relation_model.respond_to?('search_index_data')
+          value = relation_model.send('search_index_data')
+        end
+        next if !value
+
+        # save name of ref object
+        attributes_new[ attribute_name ] = value
+        if !keep_attributes[ attribute_name_with_id.to_sym ]
+          attributes.delete(key)
+        end
+      }
+      attributes_new.merge(attributes)
+    end
   end
-
 end

+ 9 - 8
app/models/history/assets.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module History::Assets
+class History
+  module Assets
 
 =begin
 
@@ -20,14 +21,14 @@ returns
 
 =end
 
-  def assets (data)
+    def assets (data)
 
-    if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ]
-      user = User.lookup( id: self['created_by_id'] )
-      data = user.assets( data )
-    end
+      if !data[ User.to_app_model ] || !data[ User.to_app_model ][ self['created_by_id'] ]
+        user = User.lookup( id: self['created_by_id'] )
+        data = user.assets( data )
+      end
 
-    data
+      data
+    end
   end
-
 end

+ 27 - 26
app/models/organization/assets.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module Organization::Assets
+class Organization
+  module Assets
 
 =begin
 
@@ -20,33 +21,33 @@ returns
 
 =end
 
-  def assets (data)
+    def assets (data)
 
-    if !data[ Organization.to_app_model ]
-      data[ Organization.to_app_model ] = {}
-    end
-    if !data[ User.to_app_model ]
-      data[ User.to_app_model ] = {}
-    end
-    if !data[ Organization.to_app_model ][ self.id ]
-      data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations
-      if data[ Organization.to_app_model ][ self.id ]['member_ids']
-        data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id|
-          if !data[ User.to_app_model ][ user_id ]
-            user = User.lookup( id: user_id )
-            data = user.assets( data )
-          end
-        }
+      if !data[ Organization.to_app_model ]
+        data[ Organization.to_app_model ] = {}
       end
-    end
-    ['created_by_id', 'updated_by_id'].each {|item|
-      next if !self[ item ]
-      if !data[ User.to_app_model ][ self[ item ] ]
-        user = User.lookup( id: self[ item ] )
-        data = user.assets( data )
+      if !data[ User.to_app_model ]
+        data[ User.to_app_model ] = {}
       end
-    }
-    data
+      if !data[ Organization.to_app_model ][ self.id ]
+        data[ Organization.to_app_model ][ self.id ] = self.attributes_with_associations
+        if data[ Organization.to_app_model ][ self.id ]['member_ids']
+          data[ Organization.to_app_model ][ self.id ]['member_ids'].each {|user_id|
+            if !data[ User.to_app_model ][ user_id ]
+              user = User.lookup( id: user_id )
+              data = user.assets( data )
+            end
+          }
+        end
+      end
+      ['created_by_id', 'updated_by_id'].each {|item|
+        next if !self[ item ]
+        if !data[ User.to_app_model ][ self[ item ] ]
+          user = User.lookup( id: self[ item ] )
+          data = user.assets( data )
+        end
+      }
+      data
+    end
   end
-
 end

+ 17 - 16
app/models/organization/permission.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module Organization::Permission
+class Organization
+  module Permission
 
 =begin
 
@@ -15,24 +16,24 @@ returns
 
 =end
 
-  def permission (data)
+    def permission (data)
 
-    # check customer
-    if data[:current_user].is_role('Customer')
+      # check customer
+      if data[:current_user].is_role('Customer')
 
-      # access ok if its own organization
-      return false if data[:type] != 'ro'
-      return false if !data[:current_user].organization_id
-      return true if self.id == data[:current_user].organization_id
+        # access ok if its own organization
+        return false if data[:type] != 'ro'
+        return false if !data[:current_user].organization_id
+        return true if self.id == data[:current_user].organization_id
 
-      # no access
-      return false
-    end
+        # no access
+        return false
+      end
 
-    # check agent
-    return true if data[:current_user].is_role(Z_ROLENAME_ADMIN)
-    return true if data[:current_user].is_role('Agent')
-    return false
+      # check agent
+      return true if data[:current_user].is_role(Z_ROLENAME_ADMIN)
+      return true if data[:current_user].is_role('Agent')
+      false
+    end
   end
-
 end

+ 42 - 40
app/models/organization/search.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module Organization::Search
+class Organization
+  module Search
 
 =begin
 
@@ -18,52 +19,53 @@ returns
 
 =end
 
-  def search(params)
+    def search(params)
 
-    # get params
-    query = params[:query]
-    limit = params[:limit] || 10
-    current_user = params[:current_user]
+      # get params
+      query = params[:query]
+      limit = params[:limit] || 10
+      current_user = params[:current_user]
 
-    # enable search only for agents and admins
-    return [] if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN)
+      # enable search only for agents and admins
+      return [] if !current_user.is_role('Agent') && !current_user.is_role(Z_ROLENAME_ADMIN)
 
-    # try search index backend
-    if SearchIndexBackend.enabled?
-      items = SearchIndexBackend.search( query, limit, 'Organization' )
-      organizations = []
-      items.each { |item|
-        organizations.push Organization.lookup( id: item[:id] )
-      }
-      return organizations
-    end
+      # try search index backend
+      if SearchIndexBackend.enabled?
+        items = SearchIndexBackend.search( query, limit, 'Organization' )
+        organizations = []
+        items.each { |item|
+          organizations.push Organization.lookup( id: item[:id] )
+        }
+        return organizations
+      end
 
-    # fallback do sql query
-    # - stip out * we already search for *query* -
-    query.gsub! '*', ''
-    organizations = Organization.where(
-      'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
-    ).order('name').limit(limit)
+      # fallback do sql query
+      # - stip out * we already search for *query* -
+      query.gsub! '*', ''
+      organizations = Organization.where(
+        'name LIKE ? OR note LIKE ?', "%#{query}%", "%#{query}%"
+      ).order('name').limit(limit)
 
-    # if only a few organizations are found, search for names of users
-    if organizations.length <= 3
-      organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
-        'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
-      ).order('organizations.name').limit(limit)
-      organizations_by_user.each {|organization_by_user|
-        organization_exists = false
-        organizations.each {|organization|
-          if organization.id == organization_by_user.id
-            organization_exists = true
+      # if only a few organizations are found, search for names of users
+      if organizations.length <= 3
+        organizations_by_user = Organization.select('DISTINCT(organizations.id)').joins('LEFT OUTER JOIN users ON users.organization_id = organizations.id').where(
+          'users.firstname LIKE ? or users.lastname LIKE ? or users.email LIKE ?', "%#{query}%", "%#{query}%", "%#{query}%"
+        ).order('organizations.name').limit(limit)
+        organizations_by_user.each {|organization_by_user|
+          organization_exists = false
+          organizations.each {|organization|
+            if organization.id == organization_by_user.id
+              organization_exists = true
+            end
+          }
+
+          # get model with full data
+          if !organization_exists
+            organizations.push Organization.find(organization_by_user)
           end
         }
-
-        # get model with full data
-        if !organization_exists
-          organizations.push Organization.find(organization_by_user)
-        end
-      }
+      end
+      organizations
     end
-    organizations
   end
 end

+ 44 - 43
app/models/organization/search_index.rb

@@ -1,6 +1,7 @@
 # Copyright (C) 2012-2014 Zammad Foundation, http://zammad-foundation.org/
 
-module Organization::SearchIndex
+class Organization
+  module SearchIndex
 
 =begin
 
@@ -14,47 +15,47 @@ returns
 
 =end
 
-  def search_index_attribute_lookup(attributes, ref_object)
-    attributes_new = {}
-    attributes.each {|key, value|
-      next if !value
-
-      # get attribute name
-      attribute_name = key.to_s
-      next if attribute_name[-3, 3] != '_id'
-      attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
-
-      # check if attribute method exists
-      next if !ref_object.respond_to?( attribute_name )
-
-      # check if method has own class
-      relation_class = ref_object.send(attribute_name).class
-      next if !relation_class
-
-      # lookup ref object
-      relation_model = relation_class.lookup( id: value )
-      next if !relation_model
-
-      # get name of ref object
-      value = nil
-      if relation_model.respond_to?('search_index_data')
-        value = relation_model.send('search_index_data')
-      end
-      next if !value
-
-      # save name of ref object
-      attributes_new[ attribute_name ] = value
-      attributes.delete(key)
-    }
-
-    # add org member for search index data
-    attributes['member'] = []
-    users = User.where( organization_id: self.id )
-    users.each { |user|
-      attributes['member'].push user.search_index_data
-    }
-
-    attributes_new.merge(attributes)
+    def search_index_attribute_lookup(attributes, ref_object)
+      attributes_new = {}
+      attributes.each {|key, value|
+        next if !value
+
+        # get attribute name
+        attribute_name = key.to_s
+        next if attribute_name[-3, 3] != '_id'
+        attribute_name = attribute_name[ 0, attribute_name.length - 3 ]
+
+        # check if attribute method exists
+        next if !ref_object.respond_to?( attribute_name )
+
+        # check if method has own class
+        relation_class = ref_object.send(attribute_name).class
+        next if !relation_class
+
+        # lookup ref object
+        relation_model = relation_class.lookup( id: value )
+        next if !relation_model
+
+        # get name of ref object
+        value = nil
+        if relation_model.respond_to?('search_index_data')
+          value = relation_model.send('search_index_data')
+        end
+        next if !value
+
+        # save name of ref object
+        attributes_new[ attribute_name ] = value
+        attributes.delete(key)
+      }
+
+      # add org member for search index data
+      attributes['member'] = []
+      users = User.where( organization_id: self.id )
+      users.each { |user|
+        attributes['member'].push user.search_index_data
+      }
+
+      attributes_new.merge(attributes)
+    end
   end
-
 end

Some files were not shown because too many files changed in this diff