Evaluation of $ LABEL inside an erb element in fixtures

Looking for a way for DRY fixtures. Say we have some data. There is a part that can be referenced from $ LABEL. Here is an example:

DEFAULTS: &DEFAULTS
  state: $LABEL
  score: 99
one:
  <<: *DEFAULTS
  uuid: <%= ActiveRecord::FixtureSet.identify(:one, :uuid) %>
two:
  <<: *DEFAULTS
  uuid: <%= ActiveRecord::FixtureSet.identify(:two, :uuid) %>

Is there a DRY way of doing this. I want something like this:

DEFAULTS: &DEFAULTS
  state: $LABEL
  score: 99
  uuid: <%= ActiveRecord::FixtureSet.identify($LABEL, :uuid) %>
one:
  <<: *DEFAULTS
two:
  <<: *DEFAULTS

But this is not possible because $ LABEL is not evaluated here. Any suggestions?

+4
source share
2 answers

I just struggled with the same problem and really don't want to switch to factories for only a few cases where it would be useful.

, , uuid , ? uuids ( Postgres), , , , Rails . primary_key , uuid, , , $LABEL .

0

, ERB $LABEL.

ActiveRecord:

https://github.com/rails/rails/blob/d514ce9199fac58e569482dc901e53d0526abdf7/activerecord/lib/active_record/fixtures.rb#L656

663 , , $PRIMARY_KEY YAML, $LABEL.

# interpolate the primary key
row.each do |key, value|
  row[key] = value.gsub("$PRIMARY_KEY", row[primary_key_name].to_s) if value.is_a?(String)
end

, , , - .

0

Source: https://habr.com/ru/post/1681097/


All Articles