I would like to add some methods to some AR Models of my App; but I think they should only be available under some circumstances; this requires some meta-programming.
So I'd like to have a file where I put all my debug methods, the only question is where to put it?
Example, I have the models:
class Admin::Restaurant < ActiveRecord::Base
class Admin::Order < ActiveRecord::Base
And in my file I have (it does deppend on MetaWhere.operator_overload! initialization):
if Rails.env != 'production'
class Admin::Order
def self.mock_make
r = Restaurant.first
user_query = User.where( :created_at > "2011-04-01" )
u = user_query.first( :offset => ( user_query.count * rand ).to_i )
o = r.orders.new
o.user = u
o.value = rand(100) + rand.round(2)
if o.save
return o
else
return nil
end
end
end
end
The thing is.. I can't get it to work on /config/initializers or /app/models.
Wrap it as a external module and include it with if condition
class MyClass << ActiveRecord::Base
include MyExtraModule if Rails.env == 'development'
end
Put them in config/environments/development.rb
Related
I have an advanced search page with a seperate search controller for my contracts.
My search is working fine but is searching ALL records, i only want it to search records associated with the current user.
I am using Devise.
Below is my search.rb
class Search < ActiveRecord::Base
def search_contracts
contracts = Contract.all
contracts = contracts.where(user_id: current_user)
contracts = contracts.joins(:tenant).where(["tenants.first_name LIKE ? OR tenants.last_name LIKE ?", "%#{keywords}%", "%#{keywords}%"]) if keywords.present?
contracts = contracts.where(["balance >= ?", min_balance]) if min_balance.present?
contracts = contracts.where(["balance <= ?", max_balance]) if max_balance.present?
contracts = contracts.where(["unpaid_rent LIKE ?", unpaid_rent]) if unpaid_rent.present?
return contracts
end
end
Here is my search_controller.rb
class SearchesController < ApplicationController
def new
#search = Search.new
end
def create
#search = Search.create!(search_params)
redirect_to #search
end
def show
#search = Search.find(params[:id])
end
private
def search_params
params.require(:search).permit(:keywords, :min_balance, :max_balance, :unpaid_rent)
end
end
show page for search results
.wrapper_with_padding
#houses.clearfix
- unless #search.user_search_contracts.blank?
- #search.user_search_contracts.each do |contract|
%a{ href: (url_for [contract])}
.house
%p.end_of_contract= contract.end_of_contract
%p.balance= number_to_currency(contract.balance, :unit => "£", negative_format: "(%u%n)")
-if contract.tenant.present?
%p.tenant_id= contract.tenant.full_name
-else
%p No Tenant Assigned
-if contract.house.present?
%p.house_id= contract.house.full_house_name
-else
%p No Property Assigned
- else
%h2 Add a Contract
%p It appears you have not added any contracts
%button= link_to "New Contract", new_contract_path
= link_to "Advanced Search", new_search_path
Although, I wouldn't implement this Search functionality in this way, but ...
Try modifying what #DR7 posted, only create an instance method, not a class method.
class Search < ActiveRecord::Base
def user_search_contracts(user)
contracts = Contract.where(user_id: user.id)
contracts = contracts.joins(:tenant).where(["tenants.first_name LIKE ? OR tenants.last_name LIKE ?", "%#{keywords}%", "%#{keywords}%"]) if keywords.present?
contracts = contracts.where(["balance >= ?", min_balance]) if min_balance.present?
contracts = contracts.where(["balance <= ?", max_balance]) if max_balance.present?
contracts = contracts.where(["unpaid_rent LIKE ?", unpaid_rent]) if unpaid_rent.present?
return contracts
end
end
And inside your view ->
.wrapper_with_padding
#houses.clearfix
- unless #search.user_search_contracts(current_user).blank?
- #search.user_search_contracts(current_user).each do |contract|
%a{ href: (url_for [contract])}
.house
%p.end_of_contract= contract.end_of_contract
%p.balance= number_to_currency(contract.balance, :unit => "£", negative_format: "(%u%n)")
-if contract.tenant.present?
%p.tenant_id= contract.tenant.full_name
-else
%p No Tenant Assigned
-if contract.house.present?
%p.house_id= contract.house.full_house_name
-else
%p No Property Assigned
- else
%h2 Add a Contract
%p It appears you have not added any contracts
%button= link_to "New Contract", new_contract_path
= link_to "Advanced Search", new_search_path
Class methods vs. Instance methods
You could do a class method to your Search model.
class Search < ActiveRecord::Base
def self.user_search_contracts(user)
user ||= User.new
contracts = Contract.where(user_id: user.id)
contracts = contracts.joins(:tenant).where(["tenants.first_name LIKE ? OR tenants.last_name LIKE ?", "%#{keywords}%", "%#{keywords}%"]) if keywords.present?
contracts = contracts.where(["balance >= ?", min_balance]) if min_balance.present?
contracts = contracts.where(["balance <= ?", max_balance]) if max_balance.present?
contracts = contracts.where(["unpaid_rent LIKE ?", unpaid_rent]) if unpaid_rent.present?
return contracts
end
end
I want to create a page where it shows the resource created by other users but hide the resources created by current_user. is there a method or certain way in which I can do so?
class ExamplesController < ApplicationController
def index
#examples = Example.all.order("created_at DESC")
#creator_examples = Example.where(creator: current_user).order("created_at DESC") <---hide this!!!
end
You can simply manipulate your where clause into something like this:
def index
#examples = Example.all.order("created_at DESC")
#creator_examples = #examples.where.not(id: current_user.id)
end
This is for rails 4, if you're using rails 3
#creator_examples = Example.where("id != ?", current_user.id)
Note -> Example.all in rails 3 returns an array so you can't chain it with where
In rails3, I make same scopes in model. for example
class Common < ActiveRecord::Base
scope :recent , order('created_at DESC')
scope :before_at , lambda{|at| where("created_at < ?" , at) }
scope :after_at , lambda{|at| where("created_at > ?" , at) }
end
I want to split common scopes to module in lib. So I try like this one.
module ScopeExtension
module Timestamps
def self.included(base)
base.send :extend, ClassMethods
end
module ClassMethods
scope :recent , lambda{order('created_at DESC')}
scope :before_at , lambda{|at| where("created_at < ?" , at) }
scope :after_at , lambda{|at| where("created_at > ?" , at) }
end
end
and I write this one
class Common < ActiveRecord::Base
include ScopeExtension::Timestamps
end
But Rails show this error.
undefined method `scope' for ScopeExtension::Timestamps::ClassMethods:Module
(I didn't forget auto loading library)
How can I easily reuse common scope feature in active record?
I guess this problem to relate loading sequence. But I don't have any idea to solve.
Please hint me.
I solved this calling the scope on self.included(class):
module Timestamps
def self.included(k)
k.scope :created_yesterday, k.where("created_at" => Date.yesterday.beginning_of_day..Date.yesterday.end_of_day)
k.scope :updated_yesterday, k.where("created_at" => Date.today.beginning_of_day..Date.today.end_of_day)
k.scope :created_today, k.where("created_at" => Date.today.beginning_of_day..Date.today.end_of_day)
k.scope :updated_today, k.where("created_at" => Date.today.beginning_of_day..Date.today.end_of_day)
end
end
In Rails 3 there's no difference between a declared scope and a class method that returns an ActiveRecord::Relation, so it can be more elegant to use a mixin module:
class MyClass < ActiveRecord::Base
extend ScopeExtension::Timestamps
end
module ScopeExtension
module Timestamps
def recent
order('created_at DESC')
end
def before_at(at)
where('created_at < ?' , at)
end
def after_at(at)
where('created_at > ?' , at)
end
end
end
MyClass.after_at(2.days.ago).before_at(1.hour.ago).recent
I'm building a user ranking system, and am trying to assign user.rank values with a name.
I wanted to define something like this in my User model and then be able to reference it when displaying each user's rank, but this probably isn't the best way:
class User < ActiveRecord::Base
RANK_NAMES = {
'Peasant' => (0..75),
'Craftsman' => (76..250),
'Vassal' => (251..750),
'Noble' => (750..1500),
'Monarch' => (1501..999999)
}
Perhaps it would be better to define a method in a controller or helper like:
if user.rank == 0..75
rank_name = "Peasant"
elsif...
But not sure how to do that. Anyone have any thoughts? I'm not even sure what to call what it is I'm trying to do, thus making it difficult to research on my own.
It could be something even as simple as this, assuming user.rank exists.
class User < ActiveRecord::Base
...
def rank_name
case self.rank
when 0..75
'Peasant'
when 76..250
'Craftsman'
when 251..750
'Vassal'
when 750..1500
'Noble'
when 1501..999999
'Monarch'
end
end
...
end
If rank_name is specific to the User, I'd make it a method of User.
You could try something like below. It might give you some ideas.
class User
RANKS = [
{:name => 'Peasant', :min => 0, :max => 75},
{:name => 'Craftsman', :min => 76, :max => 250}
# ...
]
attr_accessor :rank
def rank_name
# TODO what happens if rank is out of range of all ranks or rank is nil
# or not an integer
User::RANKS[rank_index][:name]
end
private
def rank_index
User::RANKS.index { |r| (r[:min]..r[:max]).include? #rank }
end
end
user = User.new
user.rank = 76
puts user.rank_name # -> Craftsman
I'm working on a fairly simple site that allows users to choose recipe ingredients, their quantities and then shows them nutritional info based on their recipe and a large database.
Right now, I feel like I'm repeating myself a bit. I want to be able to make this "DRY" by having one method each in the Recipe and Recipe_Ingredient model that will do the same thing only accept the right parameter, which will be the type of nutrient.
Here is the relevant code in my view that currently calls two different methods (and will call more when extended to the other nutrients):
<ul>Calories <%= #recipe.total_calories %></ul>
<ul>Fat (grams) <%= #recipe.total_fat %></ul>
In my recipe model, I have methods that iterate over each of the ingredients in the recipe:
def total_calories
recipe_ingredients.to_a.sum { |i| i.total_calories }
end
def total_fat
recipe_ingredients.to_a.sum { |i| i.total_fat }
end
In the block, we call two separate methods that actually calculate the nutrients for each individual recipe ingredient:
def total_calories
ingredient.calories*ingredient.weight1*quantity/100
end
def total_fat
ingredient.fat*ingredient.weight1*quantity/100
end
This last piece is where we reference the database of ingredients. For context, here are the relationships:
class RecipeIngredient < ActiveRecord::Base
belongs_to :ingredient
belongs_to :recipe
class Recipe < ActiveRecord::Base
has_many :recipe_ingredients
Thanks in advance for any help.
Lev
The send method with a symbol parameter works well for that kind of DRY.
<ul>Calories <%= #recipe.total :calories %></ul>
<ul>Fat (grams) <%= #recipe.total :fat %></ul>
Recipe
def total(type)
recipe_ingredients.to_a.sum { |i| i.total type }
end
RecipeIngredient
def total(type)
ingredient.send(type) * ingredient.weight1 * quantity / 100
end
You could use meta programming to dynamically add the methods. Here is a start, you can get even more DRY than this.
class DynamicTotalMatch
attr_accessor :attribute
def initialize(method_sym)
if method_sym.to_s =~ /^total_of_(.*)$/
#attribute = $1.to_sym
end
end
def match?
#attribute != nil
end
end
Recipe
class Recipe
def self.method_missing(method_sym, *arguments, &block)
match = DynamicTotalMatch.new(method_sym)
if match.match?
define_dynamic_total(method_sym, match.attribute)
send(method_sym, arguments.first)
else
super
end
end
def self.respond_to?(method_sym, include_private = false)
if DynamicTotalMatch.new(method_sym).match?
true
else
super
end
end
protected
def self.define_dynamic_total(method, attribute)
class_eval <<-RUBY
def self.#{method}(#{attribute})
recipe_ingredients.to_a.sum { |i| i.send(attribute)
end
RUBY
end
end
RecipeIngredient
class RecipeIngredient
def self.method_missing(method_sym, *arguments, &block)
match = DynamicTotalMatch.new(method_sym)
if match.match?
define_dynamic_total(method_sym, match.attribute)
send(method_sym, arguments.first)
else
super
end
end
def self.respond_to?(method_sym, include_private = false)
if DynamicTotalMatch.new(method_sym).match?
true
else
super
end
end
protected
def self.define_dynamic_total(method, attribute)
class_eval <<-RUBY
def self.#{method}(#{attribute})
ingredient.send(attribute) * ingredient.weight1 * quantity / 100
end
RUBY
end
end
Example was copied from ActiveRecord and this page: http://technicalpickles.com/posts/using-method_missing-and-respond_to-to-create-dynamic-methods/