Created
December 1, 2011 15:47
-
-
Save andruby/1417710 to your computer and use it in GitHub Desktop.
Vapor: Validate Part Of Resource
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# VAlidate Part Of Resource | |
module Vapor | |
extend ActiveSupport::Concern | |
included do | |
class_attribute :target_name, :attribute_names | |
include ActiveModel::Validations | |
include ActiveModel::Conversion | |
include ActiveModel::Naming | |
end | |
def initialize(target, params = {}) | |
instance_variable_set("@#{target_name}", target) | |
attribute_names.each do |attr| | |
instance_variable_set("@#{attr}", params.try(:[], attr) || "") | |
end | |
end | |
module InstanceMethods | |
# for form helpers | |
def persisted? | |
false | |
end | |
end | |
module ClassMethods | |
def vaporize(target_name, *attributes) | |
self.target_name = target_name | |
self.attribute_names = attributes | |
attr_accessor(*attributes) | |
attr_accessor :principal | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Virtual Model for form helpers | |
class PasswordChanger | |
include Vapor | |
vaporize :user, :current_password, :new_password, :new_password_confirmation | |
validate :check_user_credentials | |
validates_length_of :new_password, :in => 8..256 | |
validates_confirmation_of :new_password | |
def change_password! | |
if valid? | |
user.set_password(new_password) | |
end | |
end | |
private | |
def check_user_credentials | |
unless user.verify_password(current_password) | |
errors.add(:current_password, :incorrect) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class UsersController <ApplicationController | |
# ... | |
def edit_password | |
@password_changer = PasswordChanger.new(@user, params[:password_changer]) | |
if request.post? && @password_changer.change_password! | |
flash.notice = "Your password has been changed" | |
redirect_to :controller => :dashboard, :action => :index | |
end | |
end | |
# ... | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
= semantic_form_for @password_changer, :url => edit_password_path do |f| | |
= f.input :current_password | |
= f.input :new_password | |
= f.input :new_password_confirmation | |
= f.submit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment