Skip to content

Instantly share code, notes, and snippets.

@simonprev
Created January 22, 2019 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simonprev/6d65fc01d6b9dd9ae64cae9dc9fe8741 to your computer and use it in GitHub Desktop.
Save simonprev/6d65fc01d6b9dd9ae64cae9dc9fe8741 to your computer and use it in GitHub Desktop.
Generate placeholder after Accent’s new feature
defmodule GeneratePlaceholders do
import Ecto.Query
alias Accent.Repo
@regex Langue.Formatter.Rails.placeholder_regex()
@document_id "85f15c8e-a5bb-4235-b04f-3ce04e672749"
def update_operations do
operations()
|> Enum.filter(&is_binary(&1.text))
|> Enum.map(fn operation ->
%{placeholders: placeholders} =
%Langue.Entry{value: operation.text}
|> Langue.Utils.Placeholders.parse(@regex)
update!(operation, placeholders)
end)
end
def update_translations do
translations()
|> Enum.map(fn translation ->
%{placeholders: placeholders} =
%Langue.Entry{value: translation.corrected_text}
|> Langue.Utils.Placeholders.parse(@regex)
update!(translation, placeholders)
end)
end
defp operations do
Repo.all(from Accent.Operation, where: [document_id: ^@document_id])
end
defp translations do
Repo.all(from Accent.Translation, where: [document_id: ^@document_id])
end
defp update!(schema, placeholders) do
schema
|> Ecto.Changeset.change(placeholders: placeholders)
|> Repo.update!()
end
end
GeneratePlaceholders.update_operations()
GeneratePlaceholders.update_translations()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment