Class: Seedie::Model::IdGenerator
- Inherits:
-
Object
- Object
- Seedie::Model::IdGenerator
- Defined in:
- lib/seedie/model/id_generator.rb
Instance Method Summary collapse
-
#initialize(model) ⇒ IdGenerator
constructor
A new instance of IdGenerator.
- #random_id ⇒ Object
- #unique_id_for(association_klass, model_id_column) ⇒ Object
Constructor Details
#initialize(model) ⇒ IdGenerator
Returns a new instance of IdGenerator.
6 7 8 |
# File 'lib/seedie/model/id_generator.rb', line 6 def initialize(model) @model = model end |
Instance Method Details
#random_id ⇒ Object
10 11 12 13 14 15 |
# File 'lib/seedie/model/id_generator.rb', line 10 def random_id id = @model.pluck(:id).sample raise InvalidAssociationConfigError, "#{@model} has no records" unless id id end |
#unique_id_for(association_klass, model_id_column) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/seedie/model/id_generator.rb', line 17 def unique_id_for(association_klass, model_id_column) unless association_klass.column_names.include?(model_id_column) raise InvalidAssociationConfigError, "#{model_id_column} does not exist in #{association_klass}" end unique_ids = @model.ids - association_klass.pluck(model_id_column) if unique_ids.empty? raise InvalidAssociationConfigError, "No unique ids for #{@model}" end unique_ids.first end |