Giter Site home page Giter Site logo

Comments (8)

shioyama avatar shioyama commented on May 18, 2024

That's an issue with uniqueness, because it checks at the database column level. Globalize required a hack to get that to work (I actually built that hack, so I know it well).

If you remove the uniqueness: true it works for presence only, right?

from mobility.

shioyama avatar shioyama commented on May 18, 2024

Here's the PR on Globalize: globalize/globalize#121

I don't want to do this type of hack within Mobility. Maybe create an external gem which you'd have to include, say mobility-uniqueness or something. Need to think about it.

I'm removing the "bug" label because I don't consider this a bug, but a feature request.

from mobility.

kevin-jj avatar kevin-jj commented on May 18, 2024

Thanks for quick response.

It seems that more compatible works need to do if friendly_id , globalize, mobility gem work together.

from mobility.

shioyama avatar shioyama commented on May 18, 2024

So I checked this quickly. Presence validation works fine as expected. Uniqueness does not work. Depending on the backend, it will either not work as expected, or it will raise an error (complaining that a column does not exist).

It seems that more compatible works need to do if friendly_id , globalize, mobility gem work together.

Are you using globalize and mobility together? That seems a bit strange.

About compatiblity, the only issue I see here is with uniqueness validation. Are there any other issues you are experiencing?

from mobility.

kevin-jj avatar kevin-jj commented on May 18, 2024

Are you using globalize and mobility together? That seems a bit strange.

About compatiblity, the only issue I see here is with uniqueness validation. Are there any other issues >you are experiencing?

Yes. I'd like to look into which gem is suitable to our application.

from mobility.

shioyama avatar shioyama commented on May 18, 2024

I see. Well, the uniqueness problem can be solved, as a plugin or extension to Mobility.

For now, if you want a quick fix, you can add this class somewhere in your application:

class MobilityUniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator
  def validate_each(record, attribute, value)
    klass = record.class
    if klass.translated_attribute_names.include?(attribute.to_s)
      relation = klass.i18n.where(attribute => value)
      relation = relation.where.not(klass.primary_key => record.id) if record.persisted?
      relation = relation.merge(options[:conditions]) if options[:conditions]

      if relation.exists?
        error_options = options.except(:case_sensitive, :scope, :conditions)
        error_options[:value] = value

        record.errors.add(attribute, :taken, error_options)
      end
    else
      super
    end
  end
end

Now in your models, before you call validates, just add this line:

class UniquenessValidator < ::MobilityUniquenessValidator; end

This is a very basic uniqueness validation. I should work with any backend other than the serialized backend, but without the scope option.

from mobility.

shioyama avatar shioyama commented on May 18, 2024

Basic uniqueness validation, including scopes, should work now using master as long as you include Mobility before adding validations to your model.

from mobility.

daniel-gato avatar daniel-gato commented on May 18, 2024

not working in rails 7 properly.

We want the model to validate the presence of all locales (name_en, name_fr, etc)

This works:

  validates :name_en, :name_fr, presence: true

This doesn't:

  validates :name_en, :name_fr, presence: true, uniqueness: true

But this works when we use only name:

  validates :name, presence: true, uniqueness: true

from mobility.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.