<commit_before>require "test_helper"

class SerializerTest < ActiveModel::TestCase
  class Model
    def initialize(hash={})
      @attributes = hash
    end

    def read_attribute_for_serialization(name)
      @attributes[name]
    end

    def as_json(*)
      { :model => "Model" }
    end
  end

  class User
    include ActiveModel::SerializerSupport

    attr_accessor :superuser

    def initialize(hash={})
      @attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "<PASSWORD>")
    end

    def read_attribute_for_serialization(name)
      @attributes[name]
    end

    def super_user?
      @superuser
    end
  end

  class Post < Model
    def initialize(attributes)
      super(attributes)
      self.comments ||= []
      self.author = nil
    end

    attr_accessor :comments, :author
    def active_model_serializer; PostSerializer; end
  end

  class Comment < Model
    def active_model_serializer; CommentSerializer; end
  end

  class UserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name

    def serializable_hash
      attributes.merge(:ok => true).merge(options[:scope])
    end
  end

  class DefaultUserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name
  end

  class MyUserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name

    def serializable_hash
      hash = attributes
      hash = hash.merge(:super_user => true) if my_user.super_user?
      hash
    end
  end

  class CommentSerializer
    def initialize(comment, options={})
      @comment = comment
    end

    def serializable_hash
      { :title => @comment.read_attribute_for_serialization(:title) }
    end

    def as_json(options=nil)
      options ||= {}
      if options[:root] == false
        serializable_hash
      else
        { :comment => serializable_hash }
      end
    end
  end

  class PostSerializer < ActiveModel::Serializer
    attributes :title, :body
    has_many :comments, :serializer => CommentSerializer
  end

  def test_scope_works_correct
    serializer = ActiveModel::Serializer.new :foo, :scope => :bar
    asser_equal serializer.scope, :bar
  end

  def test_attributes
    user = User.new
    user_serializer = DefaultUserSerializer.new(user, {})

    hash = user_serializer.as_json

    assert_equal({
      :default_user => { :first_name => "Jose", :last_name => "Valim" }
    }, hash)
  end
<commit_msg>Update master<commit_after>require "test_helper"

class SerializerTest < ActiveModel::TestCase
  class Model
    def initialize(hash={})
      @attributes = hash
    end

    def read_attribute_for_serialization(name)
      @attributes[name]
    end

    def as_json(*)
      { :model => "Model" }
    end
  end

  class User
    include ActiveModel::SerializerSupport

    attr_accessor :superuser

    def initialize(hash={})
      @attributes = hash.merge(:first_name => "Jose", :last_name => "Valim", :password => "<PASSWORD>")
    end

    def read_attribute_for_serialization(name)
      @attributes[name]
    end

    def super_user?
      @superuser
    end
  end

  class Post < Model
    def initialize(attributes)
      super(attributes)
      self.comments ||= []
      self.author = nil
    end

    attr_accessor :comments, :author
    def active_model_serializer; PostSerializer; end
  end

  class Comment < Model
    def active_model_serializer; CommentSerializer; end
  end

  class UserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name

    def serializable_hash
      attributes.merge(:ok => true).merge(options[:scope])
    end
  end

  class DefaultUserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name
  end

  class MyUserSerializer < ActiveModel::Serializer
    attributes :first_name, :last_name

    def serializable_hash
      hash = attributes
      hash = hash.merge(:super_user => true) if my_user.super_user?
      hash
    end
  end

  class CommentSerializer
    def initialize(comment, options={})
      @comment = comment
    end

    def serializable_hash
      { :title => @comment.read_attribute_for_serialization(:title) }
    end

    def as_json(options=nil)
      options ||= {}
      if options[:root] == false
        serializable_hash
      else
        { :comment => serializable_hash }
      end
    end
  end

  class PostSerializer < ActiveModel::Serializer
    attributes :title, :body
    has_many :comments, :serializer => CommentSerializer
  end

  def test_scope_works_correct
    serializer = ActiveModel::Serializer.new :foo, :scope => :bar
    assert_equal serializer.scope, :bar
  end

  def test_attributes
    user = User.new
    user_serializer = DefaultUserSerializer.new(user, {})

    hash = user_serializer.as_json

    assert_equal({
      :default_user => { :first_name => "Jose", :last_name => "Valim" }
    }, hash)
  end

{'fraction_non_alphanumeric': 0.09085328422344997, 'fraction_numerical': 0.0, 'mean_word_length': 3.0665557404326123, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 31, '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': '3184714', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1411, 'n_words': 349}