Here is a decorator
app/decorators/campaign_decorator.rb
class CampaignDecorator < Draper::Decorator
delegate_all Campaign::Campaign
def created_at
helpers.content_tag :span, class: 'time' do
object.created_at.strftime("%a %m/%d/%y")
end
end
def custom_method
'hello there!'
end
end
When I call CampaignDecorator.custom_method it doesn't find the method. Also CampaignDecorator.first.created_at returns unformated date.
Can anyone please tell what am I missing?
That is not how you use the Draper Decorator.
First things first:
CampaignDecorator.custom_method tries to find a class method called custom_method in the CampaignDecorator class. Which is definitely NOT what you want.
CampaignDecorator.first.created_at looks for objects of CampaignDecorator class and operates there ( where there are no records so first returns nil)
What you need to do is actually decorate your model. Check the documentation for that.
You first need to add the functionality to your model:
class CampaignDecorator
decorates :campaign
end
In short you could do
#campaign = Campaign.first.decorate
#campaigns = CampaignDecorator.decorate_collection(Campaign.all)
#campaigns = Campaign.scoped.decorate
Related
I am building a Rails 5.0 API and trying to have a print_it class method that runs as_json on an object. (I need a separate method to put complex logic into later)
Whenever I test it, it errors with:
NoMethodError (undefined method print_it for #<Class:0x007f7b7b092f20>):
In Model: project.rb
class Project < ApplicationRecord
def print_it
self.as_json
end
end
In controller: projects_controller.rb
class Api::V1::ProjectsController < Api::ApiController
def index
render json: Project.print_it
end
end
How can I use print_it on an object?
Project.print_it
is calling print_it on the class Project. But, you define print_it as an instance method, not a class method, here:
class Project < ApplicationRecord
def print_it
self.as_json
end
end
You probably want something more like:
class Api::V1::ProjectsController < Api::ApiController
def index
render json: #project.print_it
end
end
Naturally, you'll need to set #project.
To use print_it on an ActiveRecord_Relation called #projects, you could do something like:
#projects.map{|p| p.print_it}
You'll end up with an array.
But that might be expensive, depending on the number of projects and the nature of print_it.
How can I use print_it on an object?
You are 'using' (calling) print_it on an object. Project is an object. Just like #project is an object. You just happen to be calling print_it on an object that doesn't have print_it defined (thus the undefined method error).
I will also note that Jörg W Mittag wishes to say:
I am one of those Ruby Purists who likes to point out that there is no such thing as a class method in Ruby. I am perfectly fine, though, with using the term class method colloquially, as long as it is fully understood by all parties that it is a colloquial usage. In other words, if you know that there is no such thing as a class method and that the term "class method" is just short for "instance method of the singleton class of an object that is an instance of Class", then there is no problem. But otherwise, I have only seen it obstruct understanding.
Let it be fully understood by all parties that the term class method is used above in its colloquial sense.
I am rather new to Rails, and would greatly appreciate any bit of help. I have created the following method:
def name_fix
name = self.split
mod_name = []
name.each do |n|
n.split("")
if n[0]
n.upcase
else
n.downcase
end
mod_name.push(n)
end
mod_name.join
end
I would like to use this method in my Controller as such:
def create
#patient = Patient.new(params[:patient])
#patient.name = params[:params][:name].name_fix
if #patient.save
redirect_to patients_path
else
render :new
end
end
How can I go about accomplishing this? Will this method reside within my Model or Controller? Previously, I've run into an undefined method error.
Note: I'm sure that there is a way to better write my code. I am grateful for help with that as well.
#app/models/patient.rb
class Patient < ActiveRecord::Base
protected
def name=(value)
mod_name = []
value.split.each do |n|
n.split("")
type = n[0] ? "up" : "down"
n.send("#{type}case")
mod_name.push(n)
end
#name = mod_name.join
end
end
#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
def create
#patient = Patient.new patient_params
#patient.save ? redirect_to(patients_path) : render(:new)
end
private
def patient_params
params.require(:patient).permit(:name)
end
end
What you're doing is trying to override the setter method, which can be done using the above code. Much more efficient and out of the way.
I have created the following method
Since you're new, let me explain something else.
It is important to note where you're using this method.
You've currently put it in the model, which means you'll have to call it to manipulate some attribute / functionality of any object created with said model.
--
Models - in Rails - build the objects which populate your app. Ruby is an object orientated language, which means that every element of your program should revolve around data objects in some degree.
As you can see above, the method of building objects in your system is really about invoking classes. These classes contain methods which can be called, either at class level (IE invoking the class through the method), or at instance level (IE calling a method on an already invoked object).
This is where you get "class" methods (Model.method) and "instance" methods (#model.method) from:
#app/models/patient.rb
class Patient < ActiveRecord::Base
def explode
#this is an instance method
puts "Instance Explode"
end
def self.explode
#this is a class method
puts "Exploded"
end
end
Thus you can call the following:
#patient = Patient.find params[:id]
#patient.explode #-> "Instance explode"
Patient.explode #-> "Exploded"
--
This is important because it gives you a strict framework of where you should, and shouldn't use methods in your models.
It explains why you have controllers & helpers, and allows you to formulate the best way to structure your application as to get the most out of the least code.
For example...
Your use of #patient.name = params[:params][:name].name_fix is incorrect.
It's wrong because you're calling the instance method .name_fix on a piece of data totally unrelated to your model. If you wanted to use .name_fix in a general sense like this, you'd probably use a helper:
#app/helpers/patients_helper.rb
class PatientsHelper
def name_fix value
# stuff here
end
end
#app/controllers/patients_controller.rb
class PatientsController < ApplicationController
def create
#patient.name = name_fix params[:patient][:name]
end
end
Since you're using the method to populate the .name attribute of your model, it makes sense to override the name= setter. This will not only provide added functionality, but is much smoother and efficient than any other way.
Methods that are called directly are best put in the Controller (or in ApplicationController if you think more than one controller might want to use it).
These are methods like
# app/controllers/my_controller.rb
def foo(bar)
# do something here
end
def create
id = params[:id]
value = foo(id)
end
If you want a chained method that acts as a property method of whatever you're calling it on. Those are characteristic of how Models work - you have your main model and you call attributes or methods on the instance of that model.
# app/models/my_model.rb
def full_name
first_name + " " + last_name
end
# app/controller/my_controller.rb
def create
id = params[:id]
model = MyModel.find(id)
full_name = model.full_name
end
In your case, you want to call name_fix ON whatever is returned by params[:params][:name], which is (I'm guessing) a String.
You have two options
Modify the String class to define a method named name_fix. I highly recommend against this. It's call "monkeypatching" and shouldn't be done without good reason. Just letting you know you can do it in some cases.
Use a direct method in your controller or ApplicationController like the first example above.
#patient.name = name_fix(params[:params][:name])
Edit: As for your request about a better way to write your code... that's difficult to teach or convey in one answer. I'd say read some open source projects out there to see how people write Ruby and some common idioms used to clean up the code. To get you started, here's how I'd re-write your code
def create
#patient = Patient.new(params[:patient])
# 1. Be descriptive with your method names. `name_fix` is vague
# 2. Why is `:name` nested under another `[:params]` hash?
#patient.name = capitalize_name(params[:name])
if #patient.save
# 1. I think `patient_path` has to be singular
# 2. It needs a `Patient` object to know how to construct the URL
# e.g. `/patients/:id`
redirect_to patient_path(#patient)
else
render :new
end
end
def capitalize_name(full_name)
# Example: julio jones
#
# 1. `split` produces an array => ["julio", "jones"]
# 2. `map` applies a function (`capitalize`) to each element
# => ["Julio", "Jones"]
# 3. `join(" ")` rejoins it => "Julio Jones"
full_name.split.map(&:capitalize).join(" ")
end
Assuming your goal with the name_fix method is just to capitalize the first letter of each name, you could just pass name as an argument and store it as a private method on the Controller:
# app/controllers/patient_controller.rb
private
def name_fix(name)
name.split.map(&:capitalize).join(" ")
end
Then you could do
#patient.name = name_fix(params[:params][:name])
in the create method.
OR, you could store this method in the model:
# app/models/patient.rb
def self.name_fix(name)
name.split.map(&:capitalize).join(" ")
end
Then you could do this instead, in the controller:
#patient.name = Patient.name_fix(params[:params][:name])
I would also suggest renaming your name_fix method to something like capitalize_name.
update your create method as below
def create
#patient = Patient.new(params[:patient])
#patient.name = params[:params][:name]
#patient = #patient.name_fix
if #patient.save
redirect_to patients_path
else
render :new
end
end
It should work.
Let's say I want to redefine a method in a model:
class Model < ActiveRecord::Base
attr_accesor :model_attr
def model_attr
'redefined'
end
end
When I access it directly, it works as it is supposed to, but when I call it from the view:
f.text_field :model_attr
It doesn't. But this still works:
f.text_field :model_attr, value: #model.model_attr
So I had to dig into Rails code:
def text_field(object_name, method, options = {})
Tags::TextField.new(object_name, method, self, options).render
end
to
class TextField < Base # :nodoc:
def render
options = #options.stringify_keys
options["size"] = options["maxlength"] unless options.key?("size")
options["type"] ||= field_type
options["value"] = options.fetch("value") { value_before_type_cast(object) } unless field_type == "file"
options["value"] &&= ERB::Util.html_escape(options["value"])
add_default_name_and_id(options)
tag("input", options)
end
and
def value_before_type_cast(object)
unless object.nil?
method_before_type_cast = #method_name + "_before_type_cast"
object.respond_to?(method_before_type_cast) ?
object.send(method_before_type_cast) :
value(object)
end
end
Okay, so it looks like text_field is not accessing the attribute directly, but rather appending _before_type_cast. I've read the documentation, but still do not understand why this is necessary for #text_field? I can do this, and it works:
class Model < ActiveRecord::Base
attr_accesor :model_atr
def model_attr
'redefined'
end
def model_attr_before_type_cast
model_attr
end
end
If I redefine both methods, can I get in trouble somehow in the future? Is there a better way to do this?
The reason for using *_before_type_cast is found on the description of this commit :
Added use of *_before_type_cast for all input and text fields. This is helpful for getting "100,000" back on a integer-based
+ validation where the value would normally be "100".
I receive undefined method 'search_type' for the code below. Can you tell me what am I doing wrong here? Probably something with calling private functions, but I can't find what the problem is.
class Entry < ActiveRecord::Base
attr_accessible :content, :rank, :title, :url, :user_id
def self.search(params)
t, o = search_type(params[:type]),search_order(params[:order])
scope = self
scope = scope.where(t) if t
scope.order(o).page(params[:page]).per_page(20)
end
private
def search_order(order)
return 'comments_count DESC' if order == '1'
return 'points DESC' if order == '2'
'rank DESC'
end
def search_type(type)
return nil unless type.present?
"entry_type = #{type}"
end
end
In the controller, I have only #entries = Entry.search(params).
It's not to do with the privateness of your methods, but the fact that search is a class method, so when you call search_order from within it, it is looking for a class method called search_order but you've defined search_order as in instance method.
Make your 2 helper methods class methods and you should be ok. if you want them to be private class methods, then
class << self
def search(...)
end
private
def search_type(...)
end
def search_order(...)
end
end
If you are wondering why #entries.search(...) works it's because I assume that #entries is something like Entry.where(...) ie, a scope and you can call class methods on scopes.
search is defined as a class method, so you should call Entry.search(params) instead of #entries.search(params).
Your method is an a class method, you cant use it form instances of your class
I have a class that contains this class method:
def self.get_event_record(row, participant)
event = Event.where(
:participant_id => participant.id,
:event_type_code => row[:event_type],
:event_start_date => self.format_date(row[:event_start_date])
).first
event = Event.new(
:participant_id => participant.id,
:event_type_code => row[:event_type],
:event_start_date => self.format_date(row[:event_start_date])
) if event.blank?
event
end
And I also have, in the same class, an instance method:
def format_date(date)
parsed_date = date.split('/')
# if month or day are single digit, make them double digit with a leading zero
if parsed_date[0].split("").size == 1
parsed_date[0].insert(0, '0')
end
if parsed_date[1].split("").size == 1
parsed_date[1].insert(0, '0')
end
parsed_date[2].insert(0, '20')
formatted_date = parsed_date.rotate(-1).join("-")
formatted_date
end
I'm getting an 'undefined method' error for #format_date. (I tried it without the self in front, at first). Can you not use instance methods in class methods of the same class?
Short answer is no, you cannot use instance methods of a class inside a class method unless you have something like:
class A
def instance_method
# do stuff
end
def self.class_method
a = A.new
a.instance_method
end
end
But as far as I can see, format_date does not have to be an instance method. So
write format_date like
def self.format_date(date)
# do stuff
end
Just create class method
def self.format_date (..)
...
end
And if u need instance method, delegate it to class method
def format_date *args
self.class.format_date *args
end
And i don't think that it is good idea to call instance methods from class scope
You could do YourClassName.new.format_date(your_date), although I think it's pretty clear you should be restructuring your code - this method probably doesn't belong on an instance. Why don't you extend the Date Class, or make format_date a class method on the class you are using?
EDIT: Here are a few other things to think about with your code:
Your whole format_date method goes to a lot of lengths to manipulate dates as strings. Why not use Ruby's Date Class? Using Date.parse or Date.strptime or even "01/01/2001".to_date might be useful depending on your locale
Consider extending the String class for your method, if you really need to make your own method:
class String
def to_friendly_formatted_date
Date.strptime(self, "%d/%m/%y")
end
end
"01/08/09".to_friendly_formated_date
Your class method is crying our for the find_or_initialize_by helper methods:
self.get_event_record(row, participant)
find_or_initialize_by_participant_id_and_event_type_code_and_event_start_date(:participant_id => participant.id, :event_type_code => row[:event_type_code], :event_start_date => row[:event_start_date].to_friendly_formatted_date)
end
By god it's long, but it achieves what you're trying to do more elegantly (although I'm open to argument!)