lib/quandl/client/models/superset.rb in quandl_client-2.7.8 vs lib/quandl/client/models/superset.rb in quandl_client-2.7.9

- old
+ new

@@ -9,13 +9,19 @@ validates :code, presence: true, format: { with: Quandl::Pattern.code, message: "is invalid. Expected format: #{Quandl::Pattern.code.to_example}" } validates :column_codes, presence: true validate :column_codes_should_be_valid! def self.find_or_build( attributes={} ) - record = self.find(attributes[:id]) if attributes[:id].present? - record = self.where( attributes.slice(:code, :source_code).merge( owner: 'myself' ) ).first unless record.try(:exists?) + attrs = attributes.symbolize_keys! + # by id + record = self.find(attrs[:id]) if attrs[:id].present? + # by source_code/code + record = self.find(File.join(attrs[:source_code], attrs[:code])) if !record.try(:exists?) && attrs[:source_code].present? && attrs[:code].present? + # by code + record = self.find(attrs[:code]) if !record.try(:exists?) && attrs[:code].present? + # build record = self.new unless record.try(:exists?) - record.assign_attributes(attributes) + record.assign_attributes(attrs) record end def self.example self.new( code: "SUPERSET_EXAMPLE", name: "Superset Name", description: "Superset description", column_codes: ['NSE.OIL.1'], column_names: ['Column Name'] ) \ No newline at end of file