<commit_before>require "spec_helper"

describe Mobility::Backend::ActiveRecord::Table, orm: :active_record do
  context "in isolation" do
    let(:attributes) { ["title", "content"] }
    let(:options) { {} }
    let(:title_backend) { article.title_translations }
    let(:content_backend) { article.content_translations }
    let(:article) { Article.find_by(slug: "baz") }

    subject { article }

    before do
      stub_const 'Article', Class.new(ActiveRecord::Base)
      Article.include Mobility
      Article.translates *attributes, backend: :table, cache: false

      # create some articles
      %w[foo bar baz].each { |slug| Article.create!(slug: slug) }
    end

    describe "#read" do
      before do
        [
          { key: "title", value: "New Article", locale: "en", translatable: article },
          { key: "title", value: "新規記事", locale: "ja", translatable: article },
          { key: "content", value: "Once upon a time...", locale: "en", translatable: article },
          { key: "content", value: "昔々あるところに…", locale: "ja", translatable: article }
        ].each { |attrs| Mobility::ActiveRecord::TextTranslation.create!(attrs) }
      end

      it "returns attribute in locale from translations table" do
        expect(title_backend.read(:en)).to have_stash("New Article")
        expect(content_backend.read(:en)).to have_stash("Once upon a time...")
        expect(title_backend.read(:ja)).to have_stash("新規記事")
        expect(content_backend.read(:ja)).to have_stash("昔々あるところに…")
      end

      it "returns nil if no translation exists" do
        expect(title_backend.read(:de)).to have_stash(nil)
      end

      it "builds translation if no translation exists" do
        expect {
          title_backend.read(:de)
        }.to change(subject.send(:mobility_text_translations), :size).by(1)
      end

      describe "reading back written attributes" do
        before do
          title_backend.write(:en, "Changed Article Title")
        end

        it "returns changed value" do
          expect(title_backend.read(:en)).to have_stash("Changed Article Title")
        end
      end
    end

    describe "#write" do
      context "no translation for locale exists" do
        it "creates translation for locale" do
          expect {
            title_backend.write(:en, "New Article")
          }.to change(subject.send(:mobility_text_translations), :size).by(1)

          expect { subject.save! }.to change(Mobility::ActiveRecord::TextTranslation, :count).by(1)
        end

        it "assigns attributes to translation" do
          title_backend.write(:en, "New Article")

          translation = subject.send(:mobility_text_translations).first
          expect(translation.key).to eq("title")
          expect(translation.value).to eq("New Article")
          expect(translation.translatable).to eq(subject)
        end
      end

      context "translation for locale exists" do
        before do
          Mobility::ActiveRecord::TextTranslation.create!(
            key: "title",
            value: "foo",
            locale: "en",
            translatable: subject
          )
        end

        it "does not create new translation for locale" do
          expect {
            title_backend.write(:en, "New Article")
          }.not_to change(subject.send(:mobility_text_translations), :size)
        end

        it "updates value attribute on existing translation" do
          title_backend.write(:en, "New New Article")
          subject.save!
          subject.reload

          translation = subject.send(:mobility_text_translations).first
          expect(translation.key).to eq("title")
          expect(translation.value).to eq("New New Article")
          expect(translation.translatable).to eq(subject)
        end

        it "removes translation if assigned nil when record is saved" do
          expect {
            title_backend.write(:en, nil)
          }.not_to change(subject.send(:mobility_text_translations), :count)

          expect {
            subject.save!
          }.to change(subject.send(:mobility_text_translations), :count).by(-1)
        end
      end
    end
  end

  context "included in AR model" do
    before do
      stub_const('Article', Class.new(ActiveRecord::Base)).class_eval do
        include Mobility
        translates :title, :content, backend: :table
      end
    end

    describe "translations association" do
      it "limits association to translations with keys matching attributes" do
        # This limits the results returned by the association to only those whose keys match the set of
        # translated attributes we have defined. This matters if, say, we save some translations, then change
        # the translated attributes for the model; we should only see the new translations, not the ones
        # created earlier with different keys.
        article = Article.create(title: "Article", content: "Content")
        translation = Mobility::ActiveRecord::TextTranslation.create(key: "foo", value: "bar", locale: "en", translatable: article)
        article = Article.first
        expect(article.mobility_text_translations).not_to include(translation)
        expect(article.mobility_text_translations.count).to eq(2)
      end
    end

    describe "creating a new record with translations" do
      it "creates record and translation in current locale" do
        Mobility.locale = :en
        article = Article.create(title: "New Article", content: "Once upon a time...")
        expect(Article.count).to eq(1)
        expect(Mobility::ActiveRecord::TextTranslation.count).to eq(2)
        expect(article.send(:mobility_text_translations).size).to eq(2)
        expect(article.title).to eq("New Article")
        expect(article.content).to eq("Once upon a time...")
      end

      it "creates translations for other locales" do
        Mobility.locale = :en<commit_msg>Add table translation in separate translates call in spec<commit_after>require "spec_helper"

describe Mobility::Backend::ActiveRecord::Table, orm: :active_record do
  context "in isolation" do
    let(:attributes) { ["title", "content"] }
    let(:options) { {} }
    let(:title_backend) { article.title_translations }
    let(:content_backend) { article.content_translations }
    let(:article) { Article.find_by(slug: "baz") }

    subject { article }

    before do
      stub_const 'Article', Class.new(ActiveRecord::Base)
      Article.include Mobility
      Article.translates *attributes, backend: :table, cache: false

      # create some articles
      %w[foo bar baz].each { |slug| Article.create!(slug: slug) }
    end

    describe "#read" do
      before do
        [
          { key: "title", value: "New Article", locale: "en", translatable: article },
          { key: "title", value: "新規記事", locale: "ja", translatable: article },
          { key: "content", value: "Once upon a time...", locale: "en", translatable: article },
          { key: "content", value: "昔々あるところに…", locale: "ja", translatable: article }
        ].each { |attrs| Mobility::ActiveRecord::TextTranslation.create!(attrs) }
      end

      it "returns attribute in locale from translations table" do
        expect(title_backend.read(:en)).to have_stash("New Article")
        expect(content_backend.read(:en)).to have_stash("Once upon a time...")
        expect(title_backend.read(:ja)).to have_stash("新規記事")
        expect(content_backend.read(:ja)).to have_stash("昔々あるところに…")
      end

      it "returns nil if no translation exists" do
        expect(title_backend.read(:de)).to have_stash(nil)
      end

      it "builds translation if no translation exists" do
        expect {
          title_backend.read(:de)
        }.to change(subject.send(:mobility_text_translations), :size).by(1)
      end

      describe "reading back written attributes" do
        before do
          title_backend.write(:en, "Changed Article Title")
        end

        it "returns changed value" do
          expect(title_backend.read(:en)).to have_stash("Changed Article Title")
        end
      end
    end

    describe "#write" do
      context "no translation for locale exists" do
        it "creates translation for locale" do
          expect {
            title_backend.write(:en, "New Article")
          }.to change(subject.send(:mobility_text_translations), :size).by(1)

          expect { subject.save! }.to change(Mobility::ActiveRecord::TextTranslation, :count).by(1)
        end

        it "assigns attributes to translation" do
          title_backend.write(:en, "New Article")

          translation = subject.send(:mobility_text_translations).first
          expect(translation.key).to eq("title")
          expect(translation.value).to eq("New Article")
          expect(translation.translatable).to eq(subject)
        end
      end

      context "translation for locale exists" do
        before do
          Mobility::ActiveRecord::TextTranslation.create!(
            key: "title",
            value: "foo",
            locale: "en",
            translatable: subject
          )
        end

        it "does not create new translation for locale" do
          expect {
            title_backend.write(:en, "New Article")
          }.not_to change(subject.send(:mobility_text_translations), :size)
        end

        it "updates value attribute on existing translation" do
          title_backend.write(:en, "New New Article")
          subject.save!
          subject.reload

          translation = subject.send(:mobility_text_translations).first
          expect(translation.key).to eq("title")
          expect(translation.value).to eq("New New Article")
          expect(translation.translatable).to eq(subject)
        end

        it "removes translation if assigned nil when record is saved" do
          expect {
            title_backend.write(:en, nil)
          }.not_to change(subject.send(:mobility_text_translations), :count)

          expect {
            subject.save!
          }.to change(subject.send(:mobility_text_translations), :count).by(-1)
        end
      end
    end
  end

  context "included in AR model" do
    before do
      stub_const('Article', Class.new(ActiveRecord::Base)).class_eval do
        include Mobility
        translates :title, :content, backend: :table
        translates :subtitle, backend: :table
      end
    end

    describe "translations association" do
      it "limits association to translations with keys matching attributes" do
        # This limits the results returned by the association to only those whose keys match the set of
        # translated attributes we have defined. This matters if, say, we save some translations, then change
        # the translated attributes for the model; we should only see the new translations, not the ones
        # created earlier with different keys.
        article = Article.create(title: "Article", subtitle: "Article subtitle", content: "Content")
        translation = Mobility::ActiveRecord::TextTranslation.create(key: "foo", value: "bar", locale: "en", translatable: article)
        article = Article.first
        expect(article.mobility_text_translations).not_to include(translation)
        expect(article.mobility_text_translations.count).to eq(3)
      end
    end

    describe "creating a new record with translations" do
      it "creates record and translation in current locale" do
        Mobility.locale = :en
        article = Article.create(title: "New Article", content: "Once upon a time...")
        expect(Article.count).to eq(1)
        expect(Mobility::ActiveRecord::TextTranslation.count).to eq(2)
        expect(article.send(:mobility_text_translations).size).to eq(2)
        expect(article.title).to eq("New Article")
        expect(article.content).to eq("Once upon a time...")
      end

      it "creates translations for other locales" do
        Mobility.locale = :en
{'fraction_non_alphanumeric': 0.10615462015763877, 'fraction_numerical': 0.001341606573872212, 'mean_word_length': 2.6743684534812076, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1363974', 'n_tokens_mistral': 3295, 'n_tokens_neox': 3202, 'n_words': 934}