<commit_before>module Citygram::Models
  class Event < Sequel::Model
    many_to_one :publisher

    plugin :serialization, :geojson, :geom
    plugin :serialization, :json, :properties
    plugin :geometry_validation

    def self.from_subscription(subscription, params)
      geom = GeoRuby::GeojsonParser.new.parse(subscription.geom).as_ewkt
      params[:publisher_id] = subscription.publisher_id
      from_geom(geom, params)
    end

    def self.date_defaults
      {
        after_date:  7.days.ago,
        before_date: Time.current
      }
    end

    def self.from_geom(geom_ewkt, params)
      after_date = params[:after_date] || date_defaults[:after_date]
      before_date = params[:before_date] || date_defaults[:before_date]
      dataset.with_sql(<<-SQL, params.fetch(:publisher_id), after_date, before_date, geom_ewkt).all
        SELECT events.*
        FROM events
        WHERE events.publisher_id = ?
          AND events.created_at > ?
          AND events.created_at <= ?
          AND ST_Intersects(events.geom, ?::geometry)
        ORDER BY events.created_at DESC
      SQL
    end

    def validate<commit_msg>Prefer explicit dataset methods over class methods<commit_after>module Citygram::Models
  class Event < Sequel::Model
    many_to_one :publisher

    plugin :serialization, :geojson, :geom
    plugin :serialization, :json, :properties
    plugin :geometry_validation

    dataset_module do
      def date_defaults
        { after_date:  7.days.ago,
          before_date: DateTime.now }
      end

      def from_subscription(subscription, params)
        geom = GeoRuby::GeojsonParser.new.parse(subscription.geom).as_ewkt
        params[:publisher_id] = subscription.publisher_id
        from_geom(geom, params)
      end

      def from_geom(geom_ewkt, params)
        after_date = params[:after_date] || date_defaults[:after_date]
        before_date = params[:before_date] || date_defaults[:before_date]

        with_sql(<<-SQL, params.fetch(:publisher_id), after_date, before_date, geom_ewkt).all
          SELECT events.*
          FROM events
          WHERE events.publisher_id = ?
            AND events.created_at > ?
            AND events.created_at <= ?
            AND ST_Intersects(events.geom, ?::geometry)
          ORDER BY events.created_at DESC
        SQL
      end
    end

    def validate
{'fraction_non_alphanumeric': 0.0893236920459379, 'fraction_numerical': 0.0008507018290089324, 'mean_word_length': 2.78743961352657, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 5, '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': '5902519', 'n_tokens_mistral': 724, 'n_tokens_neox': 704, 'n_words': 146}