Questions tagged [helpermethods]

86 questions
398
votes
7 answers

Can Rails Routing Helpers (i.e. mymodel_path(model)) be Used in Models?

Say I have a Rails Model called Thing. Thing has a url attribute that can optionally be set to a URL somewhere on the Internet. In view code, I need logic that does the following: <% if thing.url.blank? %> <%= link_to('Text', thing_path(thing))…
Aaron Longwell
  • 8,153
  • 5
  • 21
  • 10
30
votes
6 answers

Organizing c# project helper or utility classes

What are some best practices for where you should have helper classes in a .NET project? Referring to classes separate from business layer stuff, but presentation and app stuff like appSetting config managers and other code that would sometimes be…
spaghetticowboy
  • 708
  • 1
  • 8
  • 22
29
votes
6 answers

Where do I put helper methods for ActionMailer views?

I have a method that takes an array of strings and joins them so they do something like this: >> my_arr => ["A", "B", "C"] >> and_join(my_arr) => "A, B, and C" Which I'd like my mailer to have access to so I can output some information into an…
aarona
  • 35,986
  • 41
  • 138
  • 186
22
votes
5 answers

Why is using static helper methods in Java bad?

I'm asking because I'm trying to use a mocking framework (Mockito) which does not allow you to mock static methods. Looking into it I've found quite a few blog posts saying that you should have as few static methods as possible, but I'm having…
odiggity
  • 4,117
  • 7
  • 34
  • 41
19
votes
9 answers

Unit test helper methods?

I have classes which previously had massive methods so I subdivided the work of this method into 'helper' methods. These helper methods are declared private to enforce encapsulation - however I want to unit test the big public methods. Is it good…
Aly
  • 15,865
  • 47
  • 119
  • 191
8
votes
3 answers

Render partial from helper_method

Ok so I have a helper method in the application controller: def run_test(test_name) #computation stuff render :partial => test_name end And I call it like so in views: <%= run_test("testpartial") %> and it renders ok with only 1 (although...…
Msencenb
  • 5,675
  • 11
  • 52
  • 84
7
votes
1 answer

How do I write methods that insert rspec examples?

In a bunch of rspec rails unit specifications I do something like: describe Foo do [:bar, :baz].each do |a| it "should have many #{a}" do Foo.should have_many(a) end end end For cleaner code I'd rather do something like: describe…
Mori
  • 27,279
  • 10
  • 68
  • 73
3
votes
1 answer

Question on using or equals (||=) in application controller

I have seen the or equals ||= often used in application controller methods to set a variable if it doesn't exist. The most recent in Railscasts 270. But I have a question.. take for example this helper method def current_user @current_user ||=…
Msencenb
  • 5,675
  • 11
  • 52
  • 84
3
votes
2 answers

Rails helper method with link_to options as optionals

I'm trying to create a helper method that can have optional arguments for link_to method. My intention is to create a helper method for different cases: # Typical case #1 (can have any number of extra arguments) <%= toolbar_item('google.com',…
Fernando
  • 325
  • 2
  • 13
3
votes
3 answers

Private helper method to capture wildcard type for generic methods

The following code does not compile in Eclipse. It says "The method putHelper(List,int,E) in the type Abc is not applicable for the arguments (List <.capture#8-of extends E>",int,E)" private void putHelper(List list, int i, E value) { …
3
votes
3 answers

User Helper Method - grabbing users whose ID doesn't match current user ID

trying to write a helper method with 3 basic conditions. adding the 3rd one, which is trying to take the current user out of the results, is resulting in an error. def male_soccer_players return User.where(:gender => "male", :soccer => true,…
brad
  • 1,675
  • 2
  • 16
  • 23
3
votes
2 answers

Calling helper method from Class method: "undefined method"

I'm getting: undefined method 'important_method' for # when I call: User.some_class_method with: # models/user.rb class User < ActiveRecord::Base include ApplicationHelper def self.some_class_method important_method() …
TomDogg
  • 3,803
  • 5
  • 33
  • 60
3
votes
4 answers

When to create helper methods and separate files

Background: I have a large (several hundred lines) class that manages a concept based on some primitive-type data structures long[] slist; //list of unique patterns (related to polyominoes) int[][][] sref;//patterns at each place…
clwhisk
  • 1,805
  • 1
  • 18
  • 17
3
votes
3 answers

Helper methods to be used in Controllers AND Views in Rails

I understand I can put a helper method in a Helper class inside the helper folder in Rails. Then that method can be used in any view. And I understand I can put methods in the ApplicationController class and that method can be used in any…
3
votes
1 answer

Custom helper methods for Rails 3.2 Migrations

I'm trying to access helper methods inside my migrations. I can't seem to get anything to work when accomplishing something like this: Migration File: require File.expand_path('lib/migration_helper') include MigrationHelper class NewTable <…
1
2 3 4 5 6