GithubHelp home page GithubHelp logo

mrflip / attr_visible Goto Github PK

View Code? Open in Web Editor NEW
7.0 3.0 2.0 164 KB

Hides given attributes from serialization (to_xml, to_json, etc) by default.

Home Page: http://github.com/mrflip/attr_visible

License: MIT License

Ruby 100.00%

attr_visible's Introduction

AttrVisible

Description

Makes it easy to list attributes that shouldn’t be serialized by to_xml and friends.

First, use the writer attr_visible to list attributes that shouldn’t be shown to the world. Second, call serialize_defaults with +:only => visible_attributes+. You may also list associations that you’d like to include, or any other argument accepted by the ActiveRecord::Serialization methods

You can only whitelist attributes.

!! Please be aware !!

Nothing enforces these attributes to be hidden in general. You should use serialize_defaults to create a whitelist of attributes, and in your views you can use visible_attributes to check whether fields are listed as visible.

Example

class User
  has_many :roles
  has_many :posts
  # User has attributes name, email, password; and has_many roles.

  # A user should to be able to modify her name and email, but not roles.
  attr_accessible       :name, :email, :password, :password_confirmation

  # Let to_xml, to_json, etc show a user's id, name, roles and date they
  # joined, but don't show their email or password visible to the world.
  attr_visible          :id,   :name,  :roles, :created_at
  # Additionally show how many posts they've made
  serialize_defaults    :only => visible_attributes, :methods => :num_posts

  #  number of posts by this user
  def num_posts
    posts.count
  end
end

Then we would get

user = User.new("name" => "David", "email" => "[email protected]", "password" => "monkey", ... )
user.to_json # {"user": { "id":1, "name":"David", "email":"[email protected]", :created_at => '...', :num_posts => 0 } }

Copyright © 2008 Philip (flip) Kromer for infochimps.org Released under the MIT license

attr_visible's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

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.