<commit_before>class Item < ActiveRecord::Base
  include PaperclipWrapper

  STATUS_NORMAL  = 10.freeze
  STATUS_LOST    = 20.freeze
  STATUS_DAMAGED = 30.freeze
  
  PURPOSE_SHARE     = 10.freeze
  PURPOSE_GIFT      = 20.freeze
  PURPOSE_SHAREAGE  = 30.freeze
  PURPOSE_COMMUNAL  = 40.freeze
  
  PURPOSES = {
    PURPOSE_SHARE => 'share',
    PURPOSE_GIFT  => 'gift'
  }

  STATUSES = {
    STATUS_NORMAL     => 'normal',
    STATUS_LOST       => 'lost',
    STATUS_DAMAGED    => 'damaged'
  }
  
  STATUSES_VISIBLE_TO_OTHER_USERS = [ STATUS_NORMAL, STATUS_DAMAGED ]
  REQUESTABLE_STATUSES = [ STATUS_NORMAL ]
  
  has_many :item_requests
  belongs_to :owner, :polymorphic => true

  has_many :activity_logs, :as => :action_object
  has_many :activity_logs_as_related, :as => :related

  has_many :event_logs, :as => :action_object
  has_many :event_logs_as_related, :as => :related

  has_attachment :photo,
                 :styles => {
                   :large => "600x600>",
                   :medium => "150x150>",
                   :small => "100x100>",
                   :square => "50x50"
                  },
                 :default_url => "/images/noimage-:style.png"

  validates_inclusion_of :purpose, :in => [PURPOSE_SHARE, PURPOSE_GIFT],
                         :message => " must be in #{PURPOSES.values.join ', '}"
  validates_presence_of :purpose, :item_type, :name, :owner_id, :owner_type, :status
  validates_inclusion_of :status, :in => STATUSES.keys, 
                         :message => " must be in #{STATUSES.values.join ', '}"
  
  after_create :create_entity_for_item
  after_create :add_to_resource_network
  after_create :item_event_log
  
  validates_attachment_size :photo, :less_than => 1.megabyte
  validates_attachment_content_type :photo, :content_type => /image\//
  
  scope :without_deleted, :conditions => { :deleted_at => nil }
  scope :only_normal, :conditions => { :status => STATUS_NORMAL }
  scope :only_lost, :conditions => { :status => STATUS_LOST }
  scope :only_damaged, :conditions => { :status => STATUS_DAMAGED }
  scope :visible_to_other_users, where("status IN (#{STATUSES_VISIBLE_TO_OTHER_USERS.join(",")})")

  def is_owner?(entity)
    self.owner == entity
  end
  
  def item_event_log
    EventLog.create_news_event_log(self.owner, nil,  self , EventType.add_item, self)
  end
  
  def has_photo?
    !self.photo_file_name.nil?
  end
  
  def add_to_resource_network
    ResourceNetwork.create!(:entity_id => self.owner.id, :entity_type_id => 1, :resource_id => self.id, :resource_type_id => 2)
  end

  def self.search(search, person_id)
    unless search.empty?
      person = Person.find_by_id(person_id)

      entity_ids = person.people_networks.trusted_personal_network.
                          select(:entity_id).
                          collect(&:entity_id)

      items_ids = ResourceNetwork.select('DISTINCT resource_id').
                                  where(:entity_id => entity_ids).
                                  collect(&:resource_id)
      
      matcher = Item.where(:id => items_ids)

      search.split(/[ ]/).each do |word|
        matcher = matcher.
          where("UPPER(item_type) like ? OR UPPER(name) like ? OR UPPER(description) like ?", 
            "%#{word.upcase}%", "%#{word.upcase}%", "%#{word.upcase}%")<commit_msg>Item search updated with filter parameter<commit_after>class Item < ActiveRecord::Base
  include PaperclipWrapper

  STATUS_NORMAL  = 10.freeze
  STATUS_LOST    = 20.freeze
  STATUS_DAMAGED = 30.freeze
  
  PURPOSE_SHARE     = 10.freeze
  PURPOSE_GIFT      = 20.freeze
  PURPOSE_SHAREAGE  = 30.freeze
  PURPOSE_COMMUNAL  = 40.freeze
  
  PURPOSES = {
    PURPOSE_SHARE => 'share',
    PURPOSE_GIFT  => 'gift'
  }

  STATUSES = {
    STATUS_NORMAL     => 'normal',
    STATUS_LOST       => 'lost',
    STATUS_DAMAGED    => 'damaged'
  }
  
  STATUSES_VISIBLE_TO_OTHER_USERS = [ STATUS_NORMAL, STATUS_DAMAGED ]
  REQUESTABLE_STATUSES = [ STATUS_NORMAL ]
  
  has_many :item_requests
  belongs_to :owner, :polymorphic => true

  has_many :activity_logs, :as => :action_object
  has_many :activity_logs_as_related, :as => :related

  has_many :event_logs, :as => :action_object
  has_many :event_logs_as_related, :as => :related

  has_attachment :photo,
                 :styles => {
                   :large => "600x600>",
                   :medium => "150x150>",
                   :small => "100x100>",
                   :square => "50x50"
                  },
                 :default_url => "/images/noimage-:style.png"

  validates_inclusion_of :purpose, :in => [PURPOSE_SHARE, PURPOSE_GIFT],
                         :message => " must be in #{PURPOSES.values.join ', '}"
  validates_presence_of :purpose, :item_type, :name, :owner_id, :owner_type, :status
  validates_inclusion_of :status, :in => STATUSES.keys, 
                         :message => " must be in #{STATUSES.values.join ', '}"
  
  after_create :create_entity_for_item
  after_create :add_to_resource_network
  after_create :item_event_log
  
  validates_attachment_size :photo, :less_than => 1.megabyte
  validates_attachment_content_type :photo, :content_type => /image\//
  
  scope :without_deleted, :conditions => { :deleted_at => nil }
  scope :only_normal, :conditions => { :status => STATUS_NORMAL }
  scope :only_lost, :conditions => { :status => STATUS_LOST }
  scope :only_damaged, :conditions => { :status => STATUS_DAMAGED }
  scope :visible_to_other_users, where("status IN (#{STATUSES_VISIBLE_TO_OTHER_USERS.join(",")})")

  def is_owner?(entity)
    self.owner == entity
  end
  
  def item_event_log
    EventLog.create_news_event_log(self.owner, nil,  self , EventType.add_item, self)
  end
  
  def has_photo?
    !self.photo_file_name.nil?
  end
  
  def add_to_resource_network
    ResourceNetwork.create!(:entity_id => self.owner.id, :entity_type_id => 1, :resource_id => self.id, :resource_type_id => 2)
  end

  def self.search(search, person_id, filter_item_type = nil)
    unless search.empty?
      person = Person.find_by_id(person_id)

      entity_ids = person.people_networks.trusted_personal_network.
                          select(:entity_id).
                          collect(&:entity_id)

      items_ids = ResourceNetwork.select('DISTINCT resource_id').
                                  where(:entity_id => entity_ids).
                                  collect(&:resource_id)
      
      matcher = filter_item_type.nil? ? Item.where(:id => items_ids).order("#{Item.table_name}.item_type ASC") : Item.where(:id => items_ids, :item_type => filter_item_type).order("#{Item.table_name}.item_type ASC")

      search.split(/[ ]/).each do |word|
        matcher = matcher.
          where("UPPER(item_type) like ? OR UPPER(name) like ? OR UPPER(description) like ?", 
            "%#{word.upcase}%", "%#{word.upcase}%", "%#{word.upcase}%")
{'fraction_non_alphanumeric': 0.12098298676748583, 'fraction_numerical': 0.011342155009451797, 'mean_word_length': 2.774972557628979, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7104950', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2312, 'n_words': 415}