<commit_before>module Web
  # While PortletPreferences acts_as_resourceful, it is a resource as the portlet it represents.
  # The ResourceCode has a 'name' value that is the Portlet's portletid.
  #
  # Also see Lportal::Portlets, that adds a method +path+.
  class PortletPreferences < ActiveRecord::Base

    acts_as_resourceful

    # this defines +path+
    include Lportal::Portlets

    # add portlets to layouts via Layout#<<
    belongs_to :layout,
      :class_name => 'Web::Layout',
      :foreign_key => 'plid'

# this is more complicated than this.
#     belongs_to :owner,
#       :class_name => 'User',
#       :foreign_key => 'ownerid'


    # Actions for Permissions.
    def self.actions
      %w{
        CONFIGURATION
        VIEW
      }
    end

    # PortletPreferences instance keeps record of unique Portlet preferences.
    #
    # Parameteres:
    #  - portletid (String) (or portlet)
    #  - portlet       Web::Portlet
    #  - preferences   String
    def initialize(params={})
      portlet = params.delete(:portlet) if params[:portlet]

      super(params)

      self.preferences ||= '<portlet-preferences />'
      self.ownerid     ||= 0 # ??
      self.ownertype   ||= 3 # ??

      # since the instance in not yet saved, the portletid might be integer
      portlet ||= Web::Portlet.find_by_portletid("#{self.portletid}")
      if portlet
        self.portlet = portlet # instantiates if applicaple
      else
        # nil is most likely an error. the portlet database is not populated?
        # in this case default to fallback action and instantiate the portlet.
        self.portletid = self.class.instance_id(self.portletid)
      end
      self
    end

    # generates a random instance id
    def self.instance_id(portletid)
      '%s_INSTANCE_%s' % [portletid, random_string(4)]
    end

    # Defines the portlet that the preferences describe.
    # Instantiates if the portlet can instantiated.
    def portlet=(portlet)
      self.portletid = (
        portlet.instanceable? ?
          self.class.instance_id(portlet.portletid) : portlet.portletid
      )
    end

    # The Web::Portlet of this "preferences".
    def portlet
      unless @portlet
        unless self.companyid
          logger.warn 'Requested portlet of portletpreferences -- yet no companyid could be fetched.'
          @portlet = nil
        else
          logger.debug 'ok'
          logger.debug self.portlet_id
          @portlet = Web::Portlet.find(:first,
            :conditions => "companyid=#{self.companyid} AND portletid='#{self.portlet_id}'")
        end
      end
      logger.debug @portlet.inspect
      return @portlet
    end

    # Various static properties of the portlet instance and the portlet in general.
    # Requires custom migrations for PortletProperties.
    # The tables within are filled by Caterpillar, since it knows the location of the
    # portlet XML files.
    #
    # This is intentionally duplicated for Web::Portlet to minimize database queries.
    def properties
      unless @properties
        @properties = Web::PortletProperties.find_by_portletid(self.portlet_id)
      end
      return @properties
    end

    # The portletid of the Web::Portlet. <tt>portletid</tt> is still the actual portletid of the instance.
    def portlet_id<commit_msg>cleanup<commit_after>module Web
  # While PortletPreferences acts_as_resourceful, it is a resource as the portlet it represents.
  # The ResourceCode has a 'name' value that is the Portlet's portletid.
  #
  # Also see Lportal::Portlets, that adds a method +path+.
  class PortletPreferences < ActiveRecord::Base

    acts_as_resourceful

    # this defines +path+
    include Lportal::Portlets

    # add portlets to layouts via Layout#<<
    belongs_to :layout,
      :class_name => 'Web::Layout',
      :foreign_key => 'plid'

# this is more complicated than this.
#     belongs_to :owner,
#       :class_name => 'User',
#       :foreign_key => 'ownerid'


    # Actions for Permissions.
    def self.actions
      %w{
        CONFIGURATION
        VIEW
      }
    end

    # PortletPreferences instance keeps record of unique Portlet preferences.
    #
    # Parameteres:
    #  - portletid (String) (or portlet)
    #  - portlet       Web::Portlet
    #  - preferences   String
    def initialize(params={})
      portlet = params.delete(:portlet) if params[:portlet]

      super(params)

      self.preferences ||= '<portlet-preferences />'
      self.ownerid     ||= 0 # ??
      self.ownertype   ||= 3 # ??

      # since the instance in not yet saved, the portletid might be integer
      portlet ||= Web::Portlet.find_by_portletid("#{self.portletid}")
      if portlet
        self.portlet = portlet # instantiates if applicaple
      else
        # nil is most likely an error. the portlet database is not populated?
        # in this case default to fallback action and instantiate the portlet.
        self.portletid = self.class.instance_id(self.portletid)
      end
      self
    end

    # generates a random instance id
    def self.instance_id(portletid)
      '%s_INSTANCE_%s' % [portletid, random_string(4)]
    end

    # Defines the portlet that the preferences describe.
    # Instantiates if the portlet can instantiated.
    def portlet=(portlet)
      self.portletid = (
        portlet.instanceable? ?
          self.class.instance_id(portlet.portletid) : portlet.portletid
      )
    end

    # The Web::Portlet of this "preferences".
    def portlet
      unless @portlet
        unless self.companyid
          logger.warn 'Requested portlet of portletpreferences -- yet no companyid could be fetched.'
          @portlet = nil
        else
          logger.debug self.portlet_id
          @portlet = Web::Portlet.find(:first,
            :conditions => "companyid=#{self.companyid} AND portletid='#{self.portlet_id}'")
        end
      end
      logger.debug @portlet.inspect
      return @portlet
    end

    # Various static properties of the portlet instance and the portlet in general.
    # Requires custom migrations for PortletProperties.
    # The tables within are filled by Caterpillar, since it knows the location of the
    # portlet XML files.
    #
    # This is intentionally duplicated for Web::Portlet to minimize database queries.
    def properties
      unless @properties
        @properties = Web::PortletProperties.find_by_portletid(self.portlet_id)
      end
      return @properties
    end

    # The portletid of the Web::Portlet. <tt>portletid</tt> is still the actual portletid of the instance.
    def portlet_id
{'fraction_non_alphanumeric': 0.08328276699029126, 'fraction_numerical': 0.0009101941747572816, 'mean_word_length': 2.5676406926406927, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1338606', 'n_tokens_mistral': 1957, 'n_tokens_neox': 1910, 'n_words': 593}