houdini/app/models/create_model.rb

67 lines
2.1 KiB
Ruby
Raw Normal View History

class CustomAssociatedValidator < ActiveModel::EachValidator #:nodoc:
def validate_each(record, attribute, value)
byebug
if Array(value).reject { |r| valid_object?(r) }.any?
record.errors.add(attribute, :invalid, **options.merge(value: value))
end
end
private
def valid_object?(record)
record.valid?
end
end
2020-04-10 19:51:34 +00:00
class CreateModel < Base
attr_accessor :nonprofit, :user
validates_presence_of :user
validates_presence_of :nonprofit
#validate_nested_attribute :user, model_class: User
# validate_nested_attribute :nonprofit, model_class: Nonprofit
validates_with CustomAssociatedValidator, attributes: [:nonprofit]
2020-04-10 19:51:34 +00:00
before_validation do
nonprofit = Nonprofit.create(nonprofit) if !nonprofit.is_a? Nonprofit
user = User.create(user) if !nonprofit.is_a? Nonprofit
end
def save
if valid?
if nonprofit.save!
if user.save!
role = user.roles.build(host: nonprofit, name: 'nonprofit_admin')
role.save!
2020-06-10 22:31:47 +00:00
billing_plan = BillingPlan.find(Houdini.default_bp.id)
2020-04-10 19:51:34 +00:00
b_sub = nonprofit.build_billing_subscription(billing_plan: billing_plan, status: 'active')
b_sub.save!
end
end
end
# rescue ActiveRecord::RecordInvalid => e
# class_to_name = { Nonprofit => 'nonprofit', User => 'user' }
# if class_to_name[e.record.class]
# errors = e.record.errors.keys.map do |k|
# errors = e.record.errors[k].uniq
# errors.map do |error|
# Grape::Exceptions::Validation.new(
# params: ["#{class_to_name[e.record.class]}[#{k}]"],
# value message: error
# )
# end
# end
# raise Grape::Exceptions::ValidationErrors.new(errors: errors.flatten)
# else
# raise e
# end
# end
end
def save!
raise 'runtime' unless save
end
end