How to stop a helper method from applying to a specific controller? - ruby-on-rails-3

I have a helper_method that allows links to escape from a subdomain. However it is impacting my videos_controller, as it essentially seems to negate the 'current_event' method when not in the events controlller.
I've tried several dozen different ways over the last 4 days to make it so I can still escape my links from the subdomain, but still allow the videos_controller to work.
I think the best way to achieve this is to exclude the videos_controller from the helper method, but I'm not sure how (or if it is actually the best way forward - I'm obviously a noob!) Any suggestions please?! Relevant code below:
module UrlHelper
def url_for(options = nil)
if request.subdomain.present? and request.subdomain.downcase != 'www' and !options.nil? and options.is_a?(Hash) and options.has_key? :only_path and options[:only_path]
options[:only_path] = false
end
super
end
end
Videos_controller
def new
if current_event?
#video = current_event.videos.new
else
#video = Video.new
end
end
def create
if current_event.present?
#video = current_event.videos.new(params[:video])
#video.user_id = current_user.id
key = get_key_from_the_cloud
#video.key = key
else
#video = current_user.videos.new(params[:video])
#video.user_id = current_user.id
key = get_key_from_the_cloud
#video.key = key
end
if #video.save
flash[:success] = "Video uploaded!"
redirect_to root_url(subdomain: => current_event.name)
else
flash[:error] = "#{#video.errors.messages}"
render :new
end
end
current_event method
def current_event
if request.subdomain.present?
#event = Event.find_by_name(request.subdomain)
end
end

Did you take a look at this post yet?
You might want to create a new function test that only does something like
module UrlHelper
def test
puts "Test is called"
end
end
If that works you know its not including that fails but it has to be the method.
Otherwise you know the module is not included and you can narrow down the search.

Related

Rails 3: accept all params except a specific value

I have a Rails 3.2.13 Application to maintenance.
Because of authorization rules i want to limit the find(params[:file_registry_id]) method to accept all parameters except 752. (Only user tehen should be able to get it.)
def show
if current_user.tehen?
#file_registry = FileRegistry.find(752)
else
#file_registry = FileRegistry.find(params[:file_registry_id])
end
#rubric = Rubric.find(params[:id])
#rubrics = expanded_rubrics #rubric.ancestors_with_self.collect(&:id)
set_favorites
render :action => 'index'
end
Is there a method available to filter an element (here id 752) from the params hash? Or what's the best way to go?
Simple solution:
def show
#file_registry = get_file_registry
#....
end
private
def get_file_registry
if current_user.tehen?
FileRegistry.find(752)
else
unless params[:file_registry_id] == FORBIDDEN_ID_FOR_GUEST
FileRegistry.find(params[:file_registry_id])
else
false
end
end
end
FORBIDDEN_ID_FOR_GUEST should be defined outside of the controller, for example inside of a initializer.
But I suggest to use a authorization library like CanCan (https://github.com/ryanb/cancan) where you can define permissions for every use case.

Rails 3 – add action in controller's from before_filter

I am trying to add a mixin to my controller dynamically depending on the request parameters like so :
# Controller
class QuantitiesController < Admin::BaseController
before_filter :extend_input_method, only: [:create, :new]
def extend_input_method
input_method = params[:input_method]
if input_method
send(:extend, "InputMethod::#{input_method.classify}".constantize)
end
end
end
# Mixin that gets included in the controller
module InputMethod::Single
include InputMethod::Helpers
def new
puts "CALLED #new" # Debug information
load_recent_entries
quantity
end
def create
#quantity = scoped_by_subject.new(process_attributes)
if #quantity.save
save_success
else
load_recent_entries
save_error
end
end
end
The new method never gets called but my template gets rendered without raising an exception, even if action_name is new and respond_to?("new") is true after extending the instance.
I'd like to understand why this isn't working and how I can achieve something similar.
This is the solution I came up with. It works for my needs.
class QuantitiesController < Admin::BaseController
before_filter :extend_input_method, only: [:create, :new]
def new
_new
end
def create
_create
end
private
def extend_input_method
input_method = params[:input_method]
extend(Dep.get("InputMethod::#{input_method.classify}")) if input_method
end
end
module InputMethod::Single
include InputMethod::Helpers
def _new
# Do stuff...
end
def _create
# Do stuff...
end
end

Rails 3 - creating own model and working with an others

I have two tables for checking views (visits of the page) - views of pic (PhotoView) in gallery and photographers(PhotographerView).
Because these two models (and tables) are the same, I want to create a model for them - something like:
class Func < ActiveRecord::Base
def self.check_views(model_view, data)
last_view = model_viewView.where('ip_address = ? AND request_url = ?', request.remote_ip, request.url).order('created_at DESC').first
unless last_view
model_view+View.new(...).save
model_view.increment_counter(:views, data.id)
else
if (DateTime.now - last_view.created_at.to_datetime) > 1.day
model_view+View.new(...).save
model_view.increment_counter(:views, data.id)
end
end #comparing dates
end
end
and call this method like:
#photo = Photo.find(params[:id])
Func.check_views('Photo', #photo)
When I try use it with the way above, I'll get the error undefined method `check_views' for Func(Table doesn't exist):Class
Could you give me a help, how to make it work?
Thank you
You can use ActiveRecord::Concern and modules to move the common functionality into one place as follows:
module CheckViews
extend ActiveSupport::Concern
module ClassMethods
# all class methods go here, if you don't have any just leave it blank
end
def check_views(data)
last_view = where('ip_address = ? AND request_url = ?', request.remote_ip, request.url).order('created_at DESC').first
unless last_view
##views_class.new(...).save
increment_counter(:views, data.id)
else
if (DateTime.now - last_view.created_at.to_datetime) > 1.day
##views_class.new(...).save
increment_counter(:views, data.id)
end
end #comparing dates
end
end
class Photo < ActiveRecord::Base
include CheckViews
end
you can now do the following:
#photo = Photo.find(params[:id])
#photo.check_views
I'd be very tempted to do this as a module extending the classes which want the Views functionality. Something like the following ought to work; but it's entirely untested and entirely unlike anything I've ever done before so it may be completely buggy. Fair warning.
module CheckViews
def self.extended(host_class)
host_class.class_variable_set("##views_class", "#{host_class}View".constantize)
end
def check_views(data)
last_view = where('ip_address = ? AND request_url = ?', request.remote_ip, request.url).order('created_at DESC').first
unless last_view
##views_class.new(...).save
increment_counter(:views, data.id)
else
if (DateTime.now - last_view.created_at.to_datetime) > 1.day
##views_class.new(...).save
increment_counter(:views, data.id)
end
end #comparing dates
end
end
class Photo < ActiveRecord::Base
extend CheckViews
...
end
(extend adds all the instance methods of the target Module as class methods of the calling class; so Photo gains Photo.check_views(data), and self in that function is the class Photo.)

record_timestamp = false not working from model

I want to track the last_login DateTime of my user, without changing the updated_at attribute.
So inside my Model attribut I put:
def login!(session)
session[:user_id] = id
User.record_timestamp = false
self.touch(:last_login_at)
User.record_timestamp = true
end
also tried, which is the same:
def login!(session)
session[:user_id] = id
self.last_login_at = Time.now
User.record_timestamps = false
self.save(:validate => false)
User.record_timestamps = true
end
But update_at column still is updated after each login.
It seems that User.record_timestamps = false doesn't have any effect when being called from the model directly. (I use to call this method from controller or rake tasks without any problem)
please don't tell me to use update_attribute :last_login_at, Time.now which in Rails 3.1 doesnt set the updated_at column: I'm using rails 3.0.9!
Any idea?
It's really more DRY for me to do this update from the model and not from any controller...
--------------------
[edit] Hummmmmm seems like a bug in rails: I have a nested Class SubUser < User.
When I replace User.record_timestamps = false by self.class.record_timestamps = false then it's working. It's quite strange because:
1) I'm calling #user.login! with a real class User (User.first.login!)
2) even if I were calling SubUser.first.login! the command User.record_timestamps should affect too SubUser class, right?
This is the way I did this before, please give a shot.
def login!(session)
session[:user_id] = id
class << self
def record_timestamps; false; end
end
self.last_login_at = Time.now
self.save(:validate => false)
class << self
remove_method :record_timestamps
end
end
Let me know if it helps you anyway.
I would try using update_attribute because it doesn't do validations so maybe it doesn't update the timestamps either. I'm not sure if it will work:
def login!(session)
update_attribute :last_login_at, Time.now
end

Rails 3 - Multi-form Wizard - Trying to edit record does not save values

I am doing a multi-form wizard, following the steps provide by Ryan Bates. Creating a new record works, so I was trying to use the same logic for when I edit a record. However, the values that I change do not change -- when I edit something from the first form, go forward then backwards, my edits do not save. Here is the code in my controller:
def edit
session[:edit] = "Only change the fields you wish to edit"
#demographic = Demographic.find(params[:id])
session[:demographic_params] ||= {}
end
def update
session[:demographic_params].deep_merge!(params[:demographic]) if params[:demographic]
#demographic = Demographic.find(params[:id])
#demographic.current_step = session[:demographic_step]
if params[:back_button]
#demographic.previous_step
elsif #demographic.last_step?
#demographic.update_attributes(params[:demographic])
updated = true
else
#demographic.next_step
end
session[:demographic_step] = #demographic.current_step
if not updated
render "edit"
else
session[:demographic_params] = session[:demographic_step] = nil
flash[:notice] = "Entry entered successfully"
redirect_to demographic_path
end
end
What should I change that allows for saving the edits?
I don't know if this will work, but I think that should be something like this to save on every "step change":
def update
session[:demographic_params].deep_merge!(params[:demographic]) if params[:demographic]
#demographic = Demographic.find(params[:id])
#demographic.current_step = session[:demographic_step]
#demographic.update_attributes(params[:demographic])
if params[:back_button]
#demographic.previous_step
elsif #demographic.last_step?
updated = true
else
#demographic.next_step
end
session[:demographic_step] = #demographic.current_step
if not updated
render "edit"
else
session[:demographic_params] = session[:demographic_step] = nil
flash[:notice] = "Entry entered successfully"
redirect_to demographic_path
end
end
I.e., move the #demographic.update_attributes outsite the "step-by-step" logic.
So, I think that you should walk this way to solve your problem.
Hope this helps.