Giter VIP home page Giter VIP logo

devise_active_resource's Introduction

This is a basic adapter for ActiveResource to (mostly) work with Devise.

  1. Add this as a submodule or stick it somewhere that will be loaded by Rails
  2. Set Devise to use this by requiring active_resource_model in config/initializers/devise.rb
  3. Subclass your model from ActiveResource::Base & include any ActiveModel components you want to use
  4. Figure out which methods to override...

Initially we found it necessary to over #save to set some of the attributes you want to pass along. Here's an example with database_authenticable

class User < ActiveResource::Base
  include ActiveModel::Validations
  include ActiveResource::Validations

  self.site = "http:g/localhost:9292"
  self.format = :json

  attr_accessor :encrypted_password, :password_salt

  devise :database_authenticable

  def save
    self.password = @attributes['password']

    # make sure encrypted password gets set
    raise "encrypted password not set" unless @encrypted_password
    @attributes['encrypted_password'] = @encrypted_password
    @attributes['password_salt'] = @password_salt

    # delete the keys you don't want to pass back
    @attributes.delete('password')
    @attributes.delete('password_confirmation')

    super

    # manually load the errors
    self.load_remote_errors(@remote_errors)
  end

  # override some of Devise's methods
  def active?
    super && special_condition_is_valid?
  end

  def special_condition_is_valid?
    false if @errors
  end

  def special_condition_is_not_valid
    @errors.full_messages
  end

  def inactive_message
    special_condition_is_valid? ? super : :special_condition_is_not_valid
  end
end

That's about as far as we got, which was getting success messages & errors back from the remote server just like you'd expect from using ActiveRecord.

Copyright Jesse Cooke (not that I really care) and released under the MIT license. Special thanks to Ed Phillips for figuring out the errors stuff.

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.