How to use first_or_create method of Sort Package

Best Active_mocker_ruby code snippet using Sort.first_or_create

util.rb

Source:util.rb Github

copy

Full Screen

...33 end34 end35 def self.create_pricing_tiers36 [["Platinium", 5],["Gold",10],["Silver", 20],["Standard", 25]].each do |tier|37 PricingTier.where(name: tier.first, gross_profit: tier.last).first_or_create38 end39 end40 def self.create_markets41 ["Chicago", "New York"].each {|m| Market.find_or_create_by_name(m)}42 end43 def self.create_ssp_persistences44 ["Huddle", "MS Monthly", "GP Catering"].each{|ssp| ssp.find_by_name(ssp).destroy} rescue nil45 SspPersistence.where(:locked => true, :ssp_persistence_type => "managed_services_reports", :name => "Huddle", :parameters => "utf8=&searchable%5Bevent_start_time_from%5D=&searchable%5Bevent_start_time_to%5D=&direction=&sort=&per_page=&searchable%5Bouter_group%5D=Sales+Rep&direction=&sort=&per_page=&searchable%5Bstatus%5D%5B%5D=&searchable%5Bstatus%5D%5B%5D=proposed&searchable%5Bstatus%5D%5B%5D=scheduled&searchable%5Bstatus%5D%5B%5D=active&searchable%5Bstatus%5D%5B%5D=in_progress&searchable%5Bstatus%5D%5B%5D=complete&searchable%5Bstatus%5D%5B%5D=final&searchable%5Bstatus%5D%5B%5D=canceled&direction=&sort=&per_page=&searchable%5Binner_group%5D=Date&direction=&sort=&per_page=&searchable%5Bproduct%5D%5B%5D=&searchable%5Bproduct%5D%5B%5D=catering&direction=&sort=&per_page=&searchable%5Bcol%5D%5B%5D=&searchable%5Bcol%5D%5B%5D=Account&searchable%5Bcol%5D%5B%5D=Date&searchable%5Bcol%5D%5B%5D=Vendors&searchable%5Bcol%5D%5B%5D=Group+Size&searchable%5Bcol%5D%5B%5D=Revenue&searchable%5Bcol%5D%5B%5D=GP&searchable%5Bcol%5D%5B%5D=Status&direction=&sort=&per_page=").first_or_create46 SspPersistence.where(:locked => true, :ssp_persistence_type => "managed_services_reports", :name => "MS Monthly", :parameters => "utf8=&searchable%5Bevent_start_time_from%5D=&searchable%5Bevent_start_time_to%5D=&direction=&sort=&per_page=&searchable%5Bouter_group%5D=Account&direction=&sort=&per_page=&searchable%5Bstatus%5D%5B%5D=&searchable%5Bstatus%5D%5B%5D=proposed&searchable%5Bstatus%5D%5B%5D=scheduled&searchable%5Bstatus%5D%5B%5D=active&searchable%5Bstatus%5D%5B%5D=in_progress&searchable%5Bstatus%5D%5B%5D=complete&searchable%5Bstatus%5D%5B%5D=final&searchable%5Bstatus%5D%5B%5D=canceled&direction=&sort=&per_page=&searchable%5Binner_group%5D=&direction=&sort=&per_page=&searchable%5Bproduct%5D%5B%5D=&searchable%5Bproduct%5D%5B%5D=catering&searchable%5Bproduct%5D%5B%5D=prepaid_popup_gold&searchable%5Bproduct%5D%5B%5D=prepaid_popup_platinum&direction=&sort=&per_page=&searchable%5Bcol%5D%5B%5D=&searchable%5Bcol%5D%5B%5D=Account&searchable%5Bcol%5D%5B%5D=Date&searchable%5Bcol%5D%5B%5D=Event+ID&searchable%5Bcol%5D%5B%5D=Sales+Rep&searchable%5Bcol%5D%5B%5D=Product&searchable%5Bcol%5D%5B%5D=Vendors&searchable%5Bcol%5D%5B%5D=Group+Size&searchable%5Bcol%5D%5B%5D=COGS&searchable%5Bcol%5D%5B%5D=Delivery+Charge&searchable%5Bcol%5D%5B%5D=Subtotal&searchable%5Bcol%5D%5B%5D=Gratuity&searchable%5Bcol%5D%5B%5D=Enterprise+Fee&searchable%5Bcol%5D%5B%5D=Revenue&searchable%5Bcol%5D%5B%5D=Tax&searchable%5Bcol%5D%5B%5D=Total+Billing&searchable%5Bcol%5D%5B%5D=GP&searchable%5Bcol%5D%5B%5D=GP%25&searchable%5Bcol%5D%5B%5D=Status&direction=&sort=&per_page=").first_or_create47 SspPersistence.where(:locked => true, :ssp_persistence_type => "managed_services_reports", :name => "GP Catering", :parameters => "utf8=&searchable%5Bevent_start_time_from%5D=&searchable%5Bevent_start_time_to%5D=&direction=&sort=&per_page=&searchable%5Bouter_group%5D=Sales+Rep&direction=&sort=&per_page=&searchable%5Bstatus%5D%5B%5D=&searchable%5Bstatus%5D%5B%5D=proposed&searchable%5Bstatus%5D%5B%5D=scheduled&searchable%5Bstatus%5D%5B%5D=active&searchable%5Bstatus%5D%5B%5D=in_progress&searchable%5Bstatus%5D%5B%5D=complete&searchable%5Bstatus%5D%5B%5D=final&direction=&sort=&per_page=&searchable%5Binner_group%5D=&direction=&sort=&per_page=&searchable%5Bproduct%5D%5B%5D=&searchable%5Bproduct%5D%5B%5D=catering&direction=&sort=&per_page=&searchable%5Bcol%5D%5B%5D=&searchable%5Bcol%5D%5B%5D=Account&searchable%5Bcol%5D%5B%5D=Date&searchable%5Bcol%5D%5B%5D=Vendors&searchable%5Bcol%5D%5B%5D=Group+Size&searchable%5Bcol%5D%5B%5D=COGS&searchable%5Bcol%5D%5B%5D=Revenue&searchable%5Bcol%5D%5B%5D=GP&searchable%5Bcol%5D%5B%5D=Status&direction=&sort=&per_page=").first_or_create48 end49 def self.create_line_item_types50 LineItemType.where(:line_item_type => "Physical Goods", :line_item_sub_type => "Menu-Item", :sku => "").first_or_create51 LineItemType.where(:line_item_type => "Physical Goods", :line_item_sub_type => "Menu-Fee", :sku => "").first_or_create52 LineItemType.where(:line_item_type => "Physical Goods", :line_item_sub_type => "Equipment", :sku => "999000").first_or_create53 LineItemType.where(:line_item_type => "Physical Goods", :line_item_sub_type => "Other", :sku => "999001").first_or_create54 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Site Fee", :sku => "999100").first_or_create55 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Management Fee", :sku => "999101").first_or_create56 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Enterprise Fee", :sku => "999102").first_or_create57 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Gratuity", :sku => "999103").first_or_create58 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Delivery Fee", :sku => "999104").first_or_create59 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Trash Fee", :sku => "999105").first_or_create60 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Other", :sku => "999106").first_or_create61 LineItemType.where(:line_item_type => "Services", :line_item_sub_type => "Service Fee", :sku => "999107").first_or_create62 LineItemType.where(:line_item_type => "Financial", :line_item_sub_type => "Credit Memo", :sku => "999200").first_or_create63 LineItemType.where(:line_item_type => "Financial", :line_item_sub_type => "Voucher", :sku => "999201").first_or_create64 LineItemType.where(:line_item_type => "Financial", :line_item_sub_type => "Subsidy", :sku => "999202").first_or_create65 LineItemType.where(:line_item_type => "Financial", :line_item_sub_type => "Non Sufficient Funds Fee", :sku => "999203").first_or_create66 LineItemType.where(:line_item_type => "Financial", :line_item_sub_type => "Other", :sku => "999204").first_or_create67 LineItemType.where(:line_item_type => "Tax", :line_item_sub_type => "Water Bottle Tax", :sku => "999300").first_or_create68 LineItemType.where(:line_item_type => "Tax", :line_item_sub_type => "Other", :sku => "999301").first_or_create69 LineItemType.where(:line_item_type => "Other", :line_item_sub_type => "Other", :sku => "999400").first_or_create70 end71 def self.create_account72 account = Account.where(name: "ACME", account_type: "residential").first_or_create73 building = Building.where(name: "1 Main St.", market_id: Market.first.id ,timezone: "Central Time (US & Canada)").first_or_create74 Location.where(name: "ACME Kitchen", account_id: account.id, building_id: building.id).first_or_create75 end76 def self.populate_catering_order_tax_column77 require "catering/order_builder"78 puts "Event\t Order\tTax (dump)\tTax (calc)\tAction"79 puts "-----\t -----\t----------\t----------\t------"80 no_associated_event = 0;81 # Loop through all orders82 Catering::Order.order(:id).all.each do |order|83 # Run this only if an associated event exists84 # (a few orders may not have associated events because of issues occurring on production)85 unless order.event.nil?86 # Run this task only if the tax column is not available87 if order.tax.nil? or order.tax == 088 order_builder = Catering::OrderBuilder.new(order).dump()...

Full Screen

Full Screen

first_or_create.rb

Source:first_or_create.rb Github

copy

Full Screen

1module NoBrainer::Criteria::FirstOrCreate2 extend ActiveSupport::Concern3 def first_or_create(create_params={}, save_options={}, &block)4 _first_or_create(create_params, save_options.merge(:save_method => :save?), &block)5 end6 def first_or_create!(create_params={}, save_options={}, &block)7 _first_or_create(create_params, save_options.merge(:save_method => :save!), &block)8 end9 def upsert(attrs, save_options={})10 _upsert(attrs, save_options.merge(:save_method => :save?, :update => true))11 end12 def upsert!(attrs, save_options={})13 _upsert(attrs, save_options.merge(:save_method => :save!, :update => true))14 end15 private16 def _upsert(attrs, save_options)17 # Note: we don't do a full cast of user_to_cast because where() takes care18 # of it. We just need to locate a suitable uniqueness validator, for which19 # we just need to translate keys.20 attrs = Hash[attrs.map { |k,v| model.association_user_to_model_cast(k.to_sym, v) }]21 unique_keys = get_model_unique_fields.detect { |keys| keys & attrs.keys == keys }22 return where(attrs.slice(*unique_keys)).__send__(:_first_or_create, attrs, save_options) if unique_keys23 # We can't do an upsert :( Let see if we can fail on a validator first...24 instance = model.new(attrs)25 unless instance.valid?26 case save_options[:save_method]27 when :save! then raise NoBrainer::Error::DocumentInvalid, instance28 when :save? then return instance29 end30 end31 raise "Could not find a uniqueness validator for the following keys: `#{attrs.keys.inspect}'."32 end33 def _first_or_create(create_params, save_options, &block)34 raise "Cannot use .raw() with .first_or_create()" if raw?35 if block && block.arity == 136 raise "When passing a block to first_or_create(), you must pass a block with no arguments.\n" +37 "The passed block must return a hash of additional attributes for create()"38 end39 save_method = save_options.delete(:save_method)40 should_update = save_options.delete(:update)41 where_params = extract_where_params()42 # Note that we are not matching a subset of the keys on the uniqueness43 # validators; we need an exact match on the keys.44 keys = where_params.keys45 unless get_model_unique_fields.include?(keys.sort)46 # We could do without a uniqueness validator, but it's much preferable to47 # have it, so that we don't conflict with others create(), not just others48 # first_or_create().49 raise "Please add the following uniqueness validator for first_or_create():\n" +50 "class #{model}\n" +51 case keys.size52 when 1 then " field :#{keys.first}, :uniq => true"53 when 2 then " field :#{keys.first}, :uniq => {:scope => :#{keys.last}}"54 else " field :#{keys.first}, :uniq => {:scope => #{keys[1..-1].inspect}}"55 end +56 "\nend"57 end58 unless model.is_root_class? || (model.superclass.fields.keys & keys).empty?59 # We can't allow the parent to share the keys we are matching on.60 # Consider this case:61 # - Base has the field :name, :uniq => true declared62 # - A < Base63 # - B < Base64 # - A.create(:name => 'x'),65 # - B.where(:name => 'x').first_or_create66 # We are forced to return nil, or raise.67 parent = model68 parent = parent.superclass while parent.superclass < NoBrainer::Document &&69 !(parent.superclass.fields.keys & keys).empty?70 raise "A polymorphic problem has been detected: The fields `#{keys.inspect}' are defined on `#{parent}'.\n" +71 "This is problematic as first_or_create() could return nil in some cases.\n" +72 "Either 1) Only define `#{keys.inspect}' on `#{model}', \n" +73 "or 2) Query the superclass, and pass :_type in first_or_create() as such:\n" +74 " `#{parent}.where(...).first_or_create(:_type => \"#{model}\")'."75 end76 # We don't want to access create_params yet, because invoking the block77 # might be costly (the user might be doing some API call or w/e), and78 # so we want to invoke the block only if necessary.79 new_instance = model.new(where_params)80 lock_key_name = model._uniqueness_key_name_from_params(where_params)81 new_instance._lock_for_uniqueness_once(lock_key_name)82 old_instance = self.first83 if old_instance84 if should_update85 old_instance.assign_attributes(create_params)86 old_instance.__send__(save_method, save_options)87 end88 return old_instance89 end90 create_params = block.call if block91 create_params = create_params.symbolize_keys92 keys_in_conflict = create_params.keys & where_params.keys93 keys_in_conflict = keys_in_conflict.reject { |k| create_params[k] == where_params[k] }94 unless keys_in_conflict.empty?95 raise "where() and first_or_create() were given conflicting values " +96 "on the following keys: #{keys_in_conflict.inspect}"97 end98 if create_params[:_type]99 # We have to recreate the instance because we are given a _type in100 # create_params specifying a subclass. We'll have to transfert the lock101 # ownership to that new instance.102 new_instance = model.model_from_attrs(create_params).new(where_params).tap do |i|103 i.locked_keys_for_uniqueness = new_instance.locked_keys_for_uniqueness104 end105 end106 new_instance.assign_attributes(create_params)107 new_instance.__send__(save_method, save_options)108 return new_instance109 ensure110 new_instance.try(:unlock_unique_fields)111 end112 def extract_where_params()113 where_clauses = finalized_criteria.options[:where_ast]114 unless where_clauses.is_a?(NoBrainer::Criteria::Where::MultiOperator) &&115 where_clauses.op == :and116 raise "Please use a query of the form `.where(...).first_or_create(...)'"117 end118 Hash[where_clauses.clauses.map do |c|119 unless c.is_a?(NoBrainer::Criteria::Where::BinaryOperator) &&120 c.op == :eq && c.key_modifier == :scalar121 # Ignore params on the subclass type, we are handling this case directly122 # in _first_or_create()123 next if c.key_path == [:_type]124 raise "Please only use equal constraints in your where() query when using first_or_create()"125 end126 raise "You may not use nested hash queries with first_or.create()" if c.key_path.size > 1127 [c.key_path.first.to_sym, c.value]128 end.compact].tap { |h| raise "Missing where() clauses for first_or_create()" if h.empty? }129 end130 def get_model_unique_fields131 [[model.pk_name]] +132 model.unique_validators133 .flat_map { |validator| validator.attributes.map { |attr| [attr, validator] } }134 .map { |f, validator| [f, *validator.scope].map(&:to_sym).sort }135 end136end...

Full Screen

Full Screen

seeds.rb

Source:seeds.rb Github

copy

Full Screen

...9 end10 end11 if defined?(Refinery::Page)12 url = "/contact_inquiries"13 page = Refinery::Page.where(:link_url => "#{url}/new").first_or_create!(14 :title => "Contact Inquiries",15 :deletable => false,16 :menu_match => "^#{url}(\/|\/.+?|)$"17 )18 thank_you_page = Refinery::Page.where(:link_url => "#{url}/thank_you").first_or_create!(19 :title => "Thank You",20 :deletable => false,21 :show_in_menu => false22 )23 thank_you_page.update_attribute(:parent, page)24 Refinery::Pages.default_parts.each do |default_page_part|25 page.parts.where(:title => default_page_part).first_or_create!(:body => nil)26 thank_you_page.parts.where(:title => default_page_part).first_or_create!(:body => nil)27 end28 end29end30(Refinery::ContactInquiries::Setting.methods.sort - Refinery::Setting.methods).each do |setting|31 Refinery::ContactInquiries::Setting.send(setting) unless setting.to_s =~ /=$/32end

Full Screen

Full Screen

first_or_create

Using AI Code Generation

copy

Full Screen

1sort = Sort.first_or_create(name: 'sort_name')2 def self.first_or_create(params)3 first(params) || create(params)4Traceback (most recent call last):51.rb:3:in `first_or_create': undefined method `first' for Sort:Class (NoMethodError)6sort = Sort.first_or_create(name: 'sort_name')7 def self.first_or_create(params)8 first(params) || create(params)

Full Screen

Full Screen

first_or_create

Using AI Code Generation

copy

Full Screen

1db = client.db("test")2coll = db.collection("test_coll")3coll.create_index([["name", Mongo::ASCENDING]])4coll.insert({name: "test1", value: 1})5doc = coll.find(name: "test1").first_or_create({name: "test1", value: 2})

Full Screen

Full Screen

first_or_create

Using AI Code Generation

copy

Full Screen

1sort = Sort.new(1, 'Test')2sorts.add(sort)3sort = sorts.find(1)4sort = sorts.find('Test')5sort = sorts.first_or_create(1, 'Test')6sort = sorts.first_or_create('Test', 1)7sort = sorts.first_or_create(2, 'Test')8sort = sorts.first_or_create('Test2', 2)9sort = sorts.first_or_create(2, 'Test2')10sort = sorts.first_or_create('Test', 2)11sort = sorts.first_or_create(2, 'Test')12sort = sorts.first_or_create('Test2', 2)13sort = sorts.first_or_create(3, 'Test3')14sort = sorts.first_or_create('Test3', 3)15sort = sorts.first_or_create(3, 'Test3')16sort = sorts.first_or_create('Test3', 3)17sort = sorts.first_or_create(3, 'Test3')18sort = sorts.first_or_create('Test3', 3)19sort = sorts.first_or_create(3, 'Test3')20sort = sorts.first_or_create('Test3',

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful