{"id":17766453,"url":"https://github.com/dmitry/attr_accessible_block","last_synced_at":"2025-10-07T18:03:55.793Z","repository":{"id":1033194,"uuid":"861606","full_name":"dmitry/attr_accessible_block","owner":"dmitry","description":"Attribute accessible block (attr_accessible with a block, dynamic)","archived":false,"fork":false,"pushed_at":"2023-01-18T22:59:28.000Z","size":33,"stargazers_count":24,"open_issues_count":5,"forks_count":4,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-27T01:47:58.978Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmitry.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2010-08-25T14:35:56.000Z","updated_at":"2019-08-13T14:36:07.000Z","dependencies_parsed_at":"2023-02-10T18:45:53.213Z","dependency_job_id":null,"html_url":"https://github.com/dmitry/attr_accessible_block","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitry%2Fattr_accessible_block","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitry%2Fattr_accessible_block/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitry%2Fattr_accessible_block/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmitry%2Fattr_accessible_block/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmitry","download_url":"https://codeload.github.com/dmitry/attr_accessible_block/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243730916,"owners_count":20338734,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-26T20:29:53.082Z","updated_at":"2025-10-07T18:03:50.755Z","avatar_url":"https://github.com/dmitry.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"AttrAccessibleBlock 0.3.2\n=========================\n\nDEPRECATED SINCE \u003e= Rails 4.x\n\n[![travis-ci status](https://secure.travis-ci.org/dmitry/attr_accessible_block.png)](http://travis-ci.org/dmitry/attr_accessible_block) [![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/dmitry/attr_accessible_block/trend.png)](https://bitdeli.com/free \"Bitdeli Badge\")\n\nTested on Rubies: 1.9.3, 2.0.0, 2.1.1 thanks to Travis!\n\n\u003e If you need same functionallity for the Rails 2.3 or Rails 3.0, then use v0.2.2, it's fully tested and ready for this oldies. New version is total rewrite of the previous plugin, but API is the same, so it's easy to migrate if needed.\n\nLatest version of the gem is only available for the Rails 3.2.x\n\nThis is an ActiveModel plugin with possibility to define block inside the `attr_accessible` class method. `attr_protected` not supported.\n\nBecause of block, it's possible to define accessibles for instances, not just for the class level.\n\nIt's also still possible to define class level accessibles, so an old `attr_accessible :name` will work.\n\nMain features:\n\n* Add an accessible attributes based on current `record` state (eg. record.new_record?)\n* Add additional variables and use it in the block (eg. current user) `ActiveModel::MassAssignmentSecurity::WhiteListBlock.add_variable(:user) { User.current || User.new }`\n* Add permanently full accessibility on defined condition (eg.user.admin?) `ActiveModel::MassAssignmentSecurity::WhiteListBlock.always_accessible { user.admin? }`\n* Check is attribute mass-assignable or no using `attr_accessible?` instance method, that returns boolean value.\n\nSee an examples to understand the conception or specs.\n\nInstallation\n============\n\n    gem install attr_accessible_block\n\nExamples\n========\n\nHow many times you had code like that:\n\n    class User \u003c ActiveRecord::Base\n      attr_accessible :password, :password_confirmation\n\n      # ...\n    end\n\nAnd in controller:\n\n    def create\n      user = User.new(params[:user])\n      user.email = params[:user][:email]\n      user.save\n\n      # ...\n    end\n\nNow it's possible to do it easier:\n\n    class User \u003c ActiveRecord::Base\n      attr_accessible do\n        add [:password, :password_confirmation]\n        add :email if record.new_record?\n      end\n    end\n\nAnd creation of the user now can be written more DRYer\n\n    user = User.create(params[:user])\n\nAnd on user update changing of email will be rejected because of `new_record?` method.\n\nSometimes you may need to check is attribute of model assignable or no (this method mostly interesting when doing form inputs). You can do it with using `attr_accessible?` method:\n\n    user.attr_accessible?(:email) # returns false\n    user.attr_accessible?(:password) # returns true\n\nHow do I add something similar to `record`, for example I want to check current users role?\n\nEasy, with `sentient_user` gem and add the code to the `config/initializers/plugins.rb` file:\n\n    ActiveModel::MassAssignmentSecurity::WhiteListBlock.add_variable(:user) { User.current || User.new }\n\nNow `user` method available, you can check:\n\n    attr_accessible do\n      add [:password, :password_confirmation]\n      add :email if record.new_record? || user.manager?\n      add [:some_secret_fields, :another] if user.manager?\n    end\n\nWhat if I want to provide an total accessibility for the admin user?\n\nJust add this code to the `config/initializers/plugins.rb` file:\n\n    ActiveModel::MassAssignmentSecurity::WhiteListBlock.always_accessible { user.admin? }\n\nIt works even with standard `attr_accessible`, look into specs to see behaviour.\n\n\u003e NOTE: if you are getting `stack level too deep` then you have recursive call of the model object in `always_accessible` or `add_variable` blocks. Try to avoid it.\n\nShould be STI compatible, but haven't tested yet. Need's feedback on this feature. Feel free to contact with me if something goes wrong.\n\nAnd there is more, you always still can use old implementation of the `attr_accessible`, just use `old_attr_accessible` method in your models.\n\nFor more answers on your questions you can look into tests and source code.\n\nUsed on http://tenerife.by\n\nCopyright (c) 2010-2012 Dmitry Polushkin, released under the MIT license\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmitry%2Fattr_accessible_block","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmitry%2Fattr_accessible_block","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmitry%2Fattr_accessible_block/lists"}