Giter Site home page Giter Site logo

activemodel-globalid's Introduction

MOVED

To GlobalID: https://github.com/rails/globalid

Active Model: GlobalID -- find models by string references

GlobalID is a way of serializing a model into a string, which can then be used to look it up later, without the caller having to know the class. This is helpful in many cases where you accept different classes of objects, but want to do so through string references.

One example is jobs. We don't want to pass in full model objects to a job queue because the marshalling of this object might well not be safe (given that the model object can hold references to database connections or other assets). So instead we pass a GlobalID that can use used to look-up the model when its time to perform the job.

Another example is a drop-down list of options with Users and Groups. By serializing both models with GlobalID, we can make a receiver that simply takes a GlobalID -- or in the case of data that's being exposed to the world, a SignedGlobalID -- and we can deal easily with objects of both classes.

Usage

You can mix in ActiveModel::GlobalIdentification into any model that supports being found with a #find(id) method. This gem will automatically include that module into ActiveRecord::Base, so all records will be able to use the following methods:

person_gid = Person.find(5).global_id         # => <#ActiveModel::GlobalID ...
person_gid.to_s 					          # => "Person-5"
ActiveModel::GlobalLocator.locate(person_gid) # => <#Person id:5 ...

Under development as a gem, targeted for Rails inclusion

GlobalID is being developed in a gem, but is intended to be included in Active Model as part of Rails 4.2.

License

Active Model: GlobalID is released under the MIT license.

activemodel-globalid's People

Contributors

andyatkinson avatar chetan avatar dhh avatar ericboehs avatar guilleiguaran avatar ifyouseewendy avatar jeremy avatar larrylv avatar rafaelfranca avatar seuros avatar yasuoza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

activemodel-globalid's Issues

undefined method `message_verifier' for nil:NilClass (NoMethodError)

Fresh rails 4.1.1 app with ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]

$ rails c

/Users/dimitar/.gem/ruby/2.1.1/gems/activemodel-globalid-0.1.0/lib/active_model/railtie.rb:3:in `<top (required)>': undefined method `message_verifier' for nil:NilClass (NoMethodError)
    from /Users/dimitar/.gem/ruby/2.1.1/gems/activerecord-4.1.1/lib/active_record/railtie.rb:3:in `require'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/activerecord-4.1.1/lib/active_record/railtie.rb:3:in `<top (required)>'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/railties-4.1.1/lib/rails/all.rb:12:in `require'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/railties-4.1.1/lib/rails/all.rb:12:in `block in <top (required)>'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/railties-4.1.1/lib/rails/all.rb:10:in `each'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/railties-4.1.1/lib/rails/all.rb:10:in `<top (required)>'
    from /Users/dimitar/code/rails/demo/config/application.rb:3:in `require'
    from /Users/dimitar/code/rails/demo/config/application.rb:3:in `<top (required)>'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:82:in `require'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:82:in `preload'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:140:in `serve'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:128:in `block in run'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `loop'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application.rb:122:in `run'
    from /Users/dimitar/.gem/ruby/2.1.1/gems/spring-1.1.3/lib/spring/application/boot.rb:18:in `<top (required)>'
    from /Users/dimitar/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from /Users/dimitar/.rubies/ruby-2.1.1/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from -e:1:in `<main>'

Without the gem:

$ rails c
Loading development environment (Rails 4.1.1)
irb(main):001:0> Rails.application.message_verifier(:signed_global_ids)
=> #<ActiveSupport::MessageVerifier:0x007f8100c8ab08 @secret="=\xC9q\xDB9\xCEKb\xE9\xC7\xFA\xEBD\x12\xA2\x96-\xAC\x1A,\x16\x12\xA7\xDCb\xBF \xB5\xAF\xA6GO5\xDF\xD3b\xE5(\xC6\x84\x1D\xC4^\x7F\xAE\xD9\xCC\xFE1\x7F\xD0O\xC5/v]im'\x1FC\x00\xD6^", @digest="SHA1", @serializer=Marshal>

EDIT:

It seems like Rails.application is not available outside initializer block or something

GlobalId for ActiveRecord::Relation

Is it planned to add global id to ActiveRecord::Relation objects? For those situations it is still needed to manually serialize/deserialize as an array of id's.

Thanks.

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.