0

I tried to put up a Ruby Gem based on Railscast Episode 033 Making a Plugin. I got the gem created and uploaded to Rubygems.org as stringify-time, but the setter method is not working.

The GitHub Repo

The main part of the gem

module StringifyTime
  def stringify_time(*names)
    names.each do |name|
      define_method "#{name}_string" do
        read_attribute(name).to_s(:db) unless read_attribute(name).nil?
      end

      define_method "#{name}_string=" do |time_str|
        begin
          write_attribute("#{name}", Time.zone.parse(time_str))
        rescue ArgumentError
          instance_variable_set("@#{name}_invalid", true)
        end
      end

      define_method "#{name}_invalid?" do
        instance_variable_get("@#{name}_invalid")
      end
    end
  end
end

ActiveRecord::Base.send(:extend, StringifyTime)

A attribute symbol is passed in and getter and setter methods are created. The getter method works, but when I try to update the attribute through the form, the data is not saved.

using the stringify_time method in the Task model

class Task < ActiveRecord::Base                                            
  validates_presence_of :name                                              
  stringify_time :due_at                                                   

  # def due_at_string                                                      
  #   due_at.to_s(:db)                                                     
  # end                                                                    

  # def due_at_string=(due_at_str)                                         
  #   self.due_at = Time.parse(due_at_str)                                 
  # rescue ArgumentError                                                   
  #   @due_at_invalid = true                                               
  # end                                                                    

  def validate                                                             
    errors.add :due_at, 'is invalid' if due_at_invalid? # @due_at_invalid  
  end                                                                      
end

When I uncomment the setter method I wrote manually, then I can edit the attribute.

1
  • 1
    Have you tried testing the generated methods from the Rails console? Also, there is quite some difference between the generated method and the hand-written one. Commented Mar 15, 2014 at 0:11

1 Answer 1

0

Right after I tried to use the generated method in the rails console like @matjis-van-zuijlen suggested in the comments, I was able to update the due_at attribute. The issue was me not whitelisting :due_at_string in the controller.

def task_params                                                                                                                                       
  params.require(:task).permit(:name, :due_at, :due_at_string)                                                                                        
end 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.