How do I get ActiveRecord attributes method functionality? I have this class:
class PurchaseForm
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name,
:surname,
:email
validates_presence_of :name
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
def initialize(attributes = {}, shop_name)
if not attributes.nil?
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
What I need to do, to have an attributes method to list all names and values from PurchaseForm object?
Here is the refactored variant:
class PurchaseForm
include ActiveModel::Model
def self.attributes
[:name, :surname, :email]
end
attr_accessor *self.attributes
# your validations
def to_hash
self.class.attributes.inject({}) do |hash, key|
hash.merge({ key => self.send(key) })
end
end
end
Now you can easily work with this class:
irb(main):001:0> a = PurchaseForm.new({ name: 'Name' })
=> #<PurchaseForm:0x00000002606b50 #name="Name">
irb(main):002:0> a.to_hash
=> {:name=>"Name", :surname=>nil, :email=>nil}
irb(main):003:0> a.email = 'user#example.com'
=> "user#example.com"
irb(main):004:0> a
=> #<PurchaseForm:0x00000002606b50 #name="Name", #email="user#example.com">
irb(main):005:0> a.to_hash
=> {:name=>"Name", :surname=>nil, :email=>"user#example.com"}
Even more, if you want to make this behaviour reusable, consider extraction of .attributes and #to_hash methods into separate module:
module AttributesHash
extend ActiveSupport::Concern
class_methods do
def attr_accessor(*args)
#attributes = args
super(*args)
end
def attributes
#attributes
end
end
included do
def to_hash
self.class.attributes.inject({}) do |hash, key|
hash.merge({ key => self.send(key) })
end
end
end
end
Now, just include it to your model and you're done:
class PurchaseForm
include ActiveModel::Model
include AttributesHash
attr_accessor :name, :surname, :email
# your validations
end
#instance_values could do the job:
class PurchaseForm
attr_accessor :name, :email
def attributes
instance_values
end
end
Output sample:
purchase.attributes #=> {"name"=>"John", "email"=>"john#example.com"}
I've managed to solve problem with this code:
class PurchaseForm
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :attributes,
:name,
:surname,
:email
validates_presence_of :name
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
def initialize(attributes = {})
#attributes = attributes
end
def persisted?
false
end
end
Let's try this
self.as_json
=> {:name=>"Name", :surname=>nil, :email=>"user#example.com"}
would it not be better to use
include ActiveModel::Serialization
def attributes
JSON.parse(self.to_json)
end
Related
In my Rails application I have this class:
class Plan
attr_reader :name, :id, :amount, :interval, :maximum, :features
def initialize(id, name, amount, interval, maximum, features)
#id = id
#name = name
#amount = amount
#interval = interval
#maximum = maximum
#features = features
end
...
end
Is there a way to DRY up this class?
You can do it in one line:
def initialize(*args)
#id, #name, #amount, #interval, #maximum, #features = args
end
If you don't mind replacing attr_reader with attr_accessor, then you can use this, which will catch up the invalid attributes that may be provided when creating a new object:
class Plan
attr_accessor :name, :id, :amount, :interval, :maximum, :features
def initialize params = {}
params.each { |key, value| send "#{key}=", value }
end
end
If you don't mind attr_accessor instead of attr_reader, then you can use Struct. That's exactly what it does: take care of all the boilerplate in the initializer.
Plan = Struct.new(:name, :id, :amount, :interval, :maximum, :features) do
# def my_other_methods
# ...
# end
end
plan = Plan.new('Joe', 1, 500)
plan.name # => "Joe"
plan.id # => 1
plan.amount # => 500
plan.interval # => nil
how about:
def initialize(params = {})
params.each{ |k,v| instance_variable_set("##{k}", v)
end
what I usually do when I have more than two classes with initializers like this:
# initialize.rb
module Initialize
def initialize(params={})
params.each do |attr, value|
public_send("#{attr}=", value)
end if params
end
end
# plan.rb
class Plan
include Initialize
attr_accessor :name, :id, :amount, :interval, :maximum, :features
def initialize(id:, name:, amount:, interval:, maximum:, features:)
# some additional initialization code could go there
super
end
end
This way individual setters could be made to additionally cleanup data, you can define defaults, or you can delegate some setters.
I have created class using Active Model,it is working as expected ,but am having few requirements
1) I want to use all method for that class.
2) Same way i want to use some query methods like where method for that class.
3) I want to create ActiveRecord::Relation so that i can do method chaining.
See my class:
require 'active_model'
require 'active_record'
class Message
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :content
validates_presence_of :name
validates_length_of :content, :maximum => 500
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
m = Message.new(:email => "test",:name => "test")
puts m.name #=> test
puts m.class #=> Message
puts m.valid? #=> true
Message.all
Message.where(:name => "test")
some more methods:
where
limit
having
group
select
order
uniq
Could you please help me to achieve this or give some guide lines.
I want to use a serializer that renders not null attributes
class PersonSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :address, :email
end
Is this possible.
Many thanks.
Solution:
class PersonSerializer < ActiveModel::Serializer
attributes :id, :name, :phone, :address, :email
def attributes
hash = super
hash.each {|key, value|
if value.nil?
hash.delete(key)
end
}
hash
end
end
From version 0.10.x of active_model_serializer gem, you have to override the method serializable_hash instead of attributes:
# place this method inside NullAttributesRemover or directly inside serializer class
def serializable_hash(adapter_options = nil, options = {}, adapter_instance = self.class.serialization_adapter_instance)
hash = super
hash.each { |key, value| hash.delete(key) if value.nil? }
hash
end
Thanks Nabila Hamdaoui for your solution.
I made it a little more reusable via modules.
null_attribute_remover.rb
module NullAttributesRemover
def attributes
hash = super
hash.each do |key, value|
if value.nil?
hash.delete(key)
end
end
hash
end
end
Usage:
swimlane_serializer.rb
class SwimlaneSerializer < ActiveModel::Serializer
include NullAttributesRemover
attributes :id, :name, :wipMaxLimit
end
class ActiveModel::Serializer
def attributes
filter(self.class._attributes.dup).each_with_object({}) do |name, hash|
val = send(name)
hash[name] = val unless val.nil?
end
end
end
Please add validation presence:true in your Person model for (:id, :name, :phone, :address, :email) attributes, so you will get not null JSON value while you render.
There is a contact page, which offers to enter name, telephone, email and message, after that it sends to an administrator's email. There is no reason to store message in DB.
Question. How to:
Use Rails validations in controller, not using model at all, OR
Use validations in model, but without any DB relations
UPD:
Model:
class ContactPageMessage
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :telephone, :email, :message
validates :name, :telephone, :email, :message, presence: true
validates :email, email_format: { :message => "Неверный формат E-mail адреса"}
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
controller:
def sendmessage
cpm = ContactPageMessage.new()
if cpm.valid?
#settings = Setting.first
if !#settings
redirect_to contacts_path, :alert => "Fail"
end
if ContactPageMessage.received(params).deliver
redirect_to contacts_path, :notice => "Success"
else
redirect_to contacts_path, :alert => "Fail"
end
else
redirect_to contacts_path, :alert => "Fail"
end
end
end
you should use model without inheriting from ActiveRecord::Base class.
class ContactPageMessage
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :whatever
validates :whatever, :presence => true
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Through this you will able to initialize new object and able to call validations on that object.
I think you have a different class name with same name, in your controller code, I can see this :
if ContactPageMessage.received(params).deliver
redirect_to contacts_path, :notice => "Success"
else
if this is your mailer class change its name to ContactPageMessageMailer. you will no loger get that error.
Hope it will help. Thanks
I would still advice you to use model, rails models doesn't have to inherit from ActiveRecord::Base.
For example:
class Contact
include ActiveModel::Validations
attr_accessor :name, :telephone, :email, :message
validates_presence_of :name, :telephone, :email, :message
validates_format_of :email, with: EMAIL_REGEXP
end
and you can use it in your controller with:
contact = Contact.new
# ...
if contact.valid?
# do something
else
# do something else
end
In your model you can add the below which will just set getter and setter method for message and you can have validation on message without having a column in the db
attr_accessor :message
validates :message, presence: true
After searching for a tableless model example I came across this code which seems to be the general consensus on how to create one.
class Item < ActiveRecord::Base
class_inheritable_accessor :columns
self.columns = []
def self.column(name, sql_type = nil, default = nil, null = true)
columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, sql_type.to_s, null)
end
def all
return []
end
column :recommendable_type, :string
#Other columns, validations and relations etc...
end
However I would also like it to function, as a model does, representing a collection of object, so that I can do Item.all.
The plan is to populate Items with files and each Item's properties will be extracted from the files.
However currently if I do Item.all I get a
Mysql2::Error Table 'test_dev.items' doesn't exist...
error.
I found an example at http://railscasts.com/episodes/219-active-model where I can use model features and then override static methods like all (should have thought of this before).
class Item
include ActiveModel::Validations
include ActiveModel::Conversion
extend ActiveModel::Naming
attr_accessor :name, :email, :content
validates_presence_of :name
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_length_of :content, :maximum => 500
class << self
def all
return []
end
end
def initialize(attributes = {})
attributes.each do |name, value|
send("#{name}=", value)
end
end
def persisted?
false
end
end
Or you could do it like this (Edge Rails only):
class Item
include ActiveModel::Model
attr_accessor :name, :email, :content
validates_presence_of :name
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\#([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_length_of :content, :maximum => 500
end
By simply including ActiveModel::Model you get all the other modules included for you. It makes for a cleaner and more explicit representation (as in this is an ActiveModel)