Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
KoRMaK
Jul 31, 2012



I have a large when/case switch in a library method. I'd like to be able to have this when statement take on extra case clauses. I tried doing it with a yield/block but it complains that my when is messed up. Example

Ruby code:
def my_method
  case my_var
    when "case1"
      ....
    when "case2"
      ....
    yield
end

def something_else
  my_method do
    case "case3"
      ....
  end
end
Would execute as if it were written like this
Ruby code:
def my_method
  case my_var
    when "case1"
      ....
    when "case2"
      ....
    case "case3"
      ....
  end
end
So my question is basically, how can I pass around code fragments that get run inline?

KoRMaK fucked around with this message at 15:43 on Oct 1, 2014

Adbot
ADBOT LOVES YOU

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Ruby code:
def foo(bar)
  case bar
    when 1
      yield "one"
    when 2
      yield "two"
  end
end

foo(1) do |num|
  puts num 
end # => "one"

foo(2) do |num|
  puts num
end # => "two"

KoRMaK
Jul 31, 2012



Smol posted:

Ruby code:
def foo(bar)
  case bar
    when 1
      yield "one"
    when 2
      yield "two"
  end
end

foo(1) do |num|
  puts num 
end # => "one"

foo(2) do |num|
  puts num
end # => "two"
Oh... Ohhhhh.

But what If I have a more than one when? I don't think it will work.

In the meantime, I created a bunch of procs in a hash and another hash with the cases and the proc hash key as a value and it's working pretty well.

xtal
Jan 9, 2011

by Fluffdaddy

KoRMaK posted:

Oh... Ohhhhh.

But what If I have a more than one when? I don't think it will work.

In the meantime, I created a bunch of procs in a hash and another hash
with the cases and the proc hash key as a value and it's working pretty
well.

You're not going to be able to do that. Wrap the method:

code:
def my_method(bar)
  case bar
    when 1
      my_other_method
    else
      their_method(bar)
  end
end
Case statements are almost always a bad thing (for reasons including this one) so if it weren't a library my suggestion would be to remove it outright. (If this is your library and the goal is to make the case statement open to external extension, replace the case statement with a hash that's made accessible by your library.)

KoRMaK
Jul 31, 2012



I'm using devise and want to set a custom password for a user. How do I do that? There is no password field, only encrypted_password. So I suppose I need to route my plain password through whatever hashing Devise uses and store that as the result in encrypted_password. Is there a Devise functionality that offers this, or how do I find out what the hashing algorithm is?

e: Oh, I'm a dope. You just set the password and confirmation password fields and then do a save on the user object.

Side question: how does inputting a password on a form get transmitted to the server? Is there js run on the client side that hashes the password before it sends?

KoRMaK fucked around with this message at 21:35 on Oct 2, 2014

xenilk
Apr 17, 2004

ERRYDAY I BE SPLIT-TONING! Honestly, its the only skill I got other than shooting the back of women and calling it "Editorial".

KoRMaK posted:

I'm using devise and want to set a custom password for a user. How do I do that? There is no password field, only encrypted_password. So I suppose I need to route my plain password through whatever hashing Devise uses and store that as the result in encrypted_password. Is there a Devise functionality that offers this, or how do I find out what the hashing algorithm is?

Side question: how does inputting a password on a form get transmitted to the server? Is there js run on the client side that hashes the password before it sends?

not sure about your main question but to answer your side question a password field acts like just any input field... it's sent without any type of encryption to the server. The hash/salt comes from the server-side code afterward.

KoRMaK
Jul 31, 2012



xenilk posted:

not sure about your main question but to answer your side question a password field acts like just any input field... it's sent without any type of encryption to the server. The hash/salt comes from the server-side code afterward.
So SSL does all the important stuff of keeping the text of the password from being intercepted as its submitted over a network?

kayakyakr
Feb 16, 2004

Kayak is true

KoRMaK posted:

So SSL does all the important stuff of keeping the text of the password from being intercepted as its submitted over a network?

yes indeed.

And you don't need to set password confirmation when you're setting a password in console, just update password.

KoRMaK
Jul 31, 2012



How do I get procs to run in the context that they are being called from, instead of the context they were written from?

Ruby code:

module Module1
  def method_a
    my_var = "some value"
    my_procs = []
    yield my_procs
    my_procs.each{ |p| p.call }
  end
end

class Klass2
  include Module1
  def method_a
    super do |my_procs|
      my_procs << Proc.new{ my_var + "some other phrase"}
    end
  end
end
I get an error reporting the my_var doesn't exist.

Jaded Burnout
Jul 10, 2004


KoRMaK posted:

How do I get procs to run in the context that they are being called from, instead of the context they were written from?

Ruby code:

module Module1
  def method_a
    my_var = "some value"
    my_procs = []
    yield my_procs
    my_procs.each{ |p| p.call }
  end
end

class Klass2
  include Module1
  def method_a
    super do |my_procs|
      my_procs << Proc.new{ my_var + "some other phrase"}
    end
  end
end
I get an error reporting the my_var doesn't exist.

That is not how closures work. If you want to pass a variable to a proc at run time then pass it as an argument to `call`, and it'll show up as a block variable in the proc.

KoRMaK
Jul 31, 2012



Arachnamus posted:

That is not how closures work. If you want to pass a variable to a proc at run time then pass it as an argument to `call`, and it'll show up as a block variable in the proc.

Oh geez, thats so many variables. I was hoping the proc would get executed in the context of where it was being called from.

e: I have a bunch of procs that will take the same parameters. Can I write a class that extends proc? I'd like to just take a block, and then when I call it pass in the params. So I'd like to make a new class that prevents me from having to declare the variables each time
Ruby code:
myproc = Proc.new { |my_var1, my_var2, my_var3| ...do stuff }
myproc.call(var1, var2, var3)
Instead I would rather do
Ruby code:
myproc = MyProc.new { ...do stuff }
myproc.call(var1, var2, var3)
The MyProc.call method would pass the variables to the supplied proc.

KoRMaK fucked around with this message at 22:58 on Oct 3, 2014

Sil
Jan 4, 2007

KoRMaK posted:

How do I get procs to run in the context that they are being called from, instead of the context they were written from?

Ruby code:

module Module1
  def method_a
    my_var = "some value"
    my_procs = []
    yield my_procs
    my_procs.each{ |p| p.call }
  end
end

class Klass2
  include Module1
  def method_a
    super do |my_procs|
      my_procs << Proc.new{ my_var + "some other phrase"}
    end
  end
end
I get an error reporting the my_var doesn't exist.
Ruby code:
my_procs.each { |p| instance_eval(&p) }
e. don't extend proc, that's crazy.

Sil fucked around with this message at 01:26 on Oct 4, 2014

KoRMaK
Jul 31, 2012



Sil posted:

Ruby code:
my_procs.each { |p| instance_eval(&p) }
That doesn't work. Am I missing something? I think I'd have to turn my_var into @my_var for this to work. I was hoping to get the nearest scoped variables from where it was ran, not where it was created. That is, I want the context to be where its run, not from where it was created.

undefined local variable or method `my_var' for #<Klass2:0xfd3fc70>

KoRMaK fucked around with this message at 03:36 on Oct 4, 2014

Jaded Burnout
Jul 10, 2004


KoRMaK posted:

Oh geez, thats so many variables. I was hoping the proc would get executed in the context of where it was being called from.

e: I have a bunch of procs that will take the same parameters. Can I write a class that extends proc? I'd like to just take a block, and then when I call it pass in the params. So I'd like to make a new class that prevents me from having to declare the variables each time
Ruby code:
myproc = Proc.new { |my_var1, my_var2, my_var3| ...do stuff }
myproc.call(var1, var2, var3)
Instead I would rather do
Ruby code:
myproc = MyProc.new { ...do stuff }
myproc.call(var1, var2, var3)
The MyProc.call method would pass the variables to the supplied proc.

I vaguely remember a few weeks back saying to you that you're going to reach a point where you get lost in the metaprogramming and everything starts to become incomprehensible. This is one of those points.

What are you actually trying to achieve?

KoRMaK
Jul 31, 2012



I have a bunch of procs that I would like to have executed in the context that they are called from so they have access to the local variables near them. They will be getting called from inside a loop, and that loops variables' values will change each iteration.

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
Just don't do that. Dynamic scoping is straight from hell.

kayakyakr
Feb 16, 2004

Kayak is true
much easier: pass an options hash to those functions that you are calling as an argument. Use the options hash inside the function.

KoRMaK
Jul 31, 2012



kayakyakr posted:

much easier: pass an options hash to those functions that you are calling as an argument. Use the options hash inside the function.
Boooooooooooooo
....
Booooo

I have a bunch of these things that I'm going to have to add block parameters too now. Dang. Sounds like the sanest approach though.

Sil
Jan 4, 2007

KoRMaK posted:

That doesn't work. Am I missing something? I think I'd have to turn my_var into @my_var for this to work. I was hoping to get the nearest scoped variables from where it was ran, not where it was created. That is, I want the context to be where its run, not from where it was created.

undefined local variable or method `my_var' for #<Klass2:0xfd3fc70>

You're a wild dude.

Ruby code:
module Module1
  def method_a
    @my_var = "some value"
    my_procs = []
    yield my_procs
    my_procs.each{ |p| puts instance_eval(&p) }
  end
end

class Klass2
  include Module1
  def method_a
    super do |my_procs|
      my_procs << Proc.new{ @my_var + "some other phrase"}
    end
  end
end
This works. So yeah, instance variables if you want a lazy way to do this.

Maybe this can help you achieve what you want http://djellemah.com/blog/2013/10/09/instance-eval-with-access-to-outside-scope/ ? It's too saturday for me to fully understand what he's writing about, frankly, but it seems vaguely related.

Pollyanna
Mar 5, 2005

Milk's on them.


Is there a particular convention when generating controllers? Way I understand it is that the basic format is rails g controller ControllerName action1 action2. Is that right? And what sorts of things need a controller? It makes sense for a controller to be called User and have actions create and delete, but what would a controller for static pages do? What other kinds of things would necessitate a controller?

kayakyakr
Feb 16, 2004

Kayak is true

Pollyanna posted:

Is there a particular convention when generating controllers? Way I understand it is that the basic format is rails g controller ControllerName action1 action2. Is that right? And what sorts of things need a controller? It makes sense for a controller to be called User and have actions create and delete, but what would a controller for static pages do? What other kinds of things would necessitate a controller?

So if you want to get a feel for controller convention, you should generate a few scaffolds instead. Scaffold will generate the model, controller, views, and everything else. It'll come with very basic features.

Rails controllers tend to be resourceful. that means that they're intended to plug in to the route like so:
Ruby code:
resources :users
# or
resource :user
Read more about routing here: http://guides.rubyonrails.org/routing.html

Generally, you want your controller to be plural, unless you're dealing with a singleton type of object. So User might be one that isn't plural. A resourceful route will have index, show, new, create, edit, update, destroy. You may not need all of those, but that's your starting set.

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb
Why does /var/lib/gems/1.9.1/doc/fog-1.23.0 take up 660MB? I have a couple different versions in there too, taking up tons of space

necrotic
Aug 2, 2005
I owe my brother big time for this!

fletcher posted:

Why does /var/lib/gems/1.9.1/doc/fog-1.23.0 take up 660MB? I have a couple different versions in there too, taking up tons of space

Fog is thoroughly documented and supports a ton of services. You often don't need the docs locally (thanks to rdoc.info), and I recommend creating a ~/.gemrc or /etc/gemrc with the following:

code:
gem: --no-ri --no-rdoc
edit: I went ahead and installed Fog 1.24.0 with ri and rdoc and my folder is only 100M. My entire `gem env gemdir`/doc dir is only 118M after installing Fog. Do you have more than just the ri/rdoc versions of the docs available in the Fog folder?

fletcher
Jun 27, 2003

ken park is my favorite movie

Cybernetic Crumb

necrotic posted:

Fog is thoroughly documented and supports a ton of services. You often don't need the docs locally (thanks to rdoc.info), and I recommend creating a ~/.gemrc or /etc/gemrc with the following:

code:
gem: --no-ri --no-rdoc
edit: I went ahead and installed Fog 1.24.0 with ri and rdoc and my folder is only 100M. My entire `gem env gemdir`/doc dir is only 118M after installing Fog. Do you have more than just the ri/rdoc versions of the docs available in the Fog folder?

Nope just the ri/rdoc versions, here's some ncdu output:

code:
--- /var/lib/gems/1.9.1/doc --------------
  662.2MiB [##########] /fog-1.23.0
  646.7MiB [######### ] /fog-1.20.0
   22.3MiB [          ] /chef-11.10.4
   12.5MiB [          ] /sass-3.3.8
    6.8MiB [          ] /treetop-1.4.15
    5.6MiB [          ] /ffi-1.9.4
code:
--- /var/lib/gems/1.9.1/doc/fog-1.23.0/rdoc ---------
                        /..
  563.5MiB [##########] /Fog
   20.3MiB [          ] /lib
    1.7MiB [          ]  index.html
    1.5MiB [          ]  Fog.html
I uninstalled both versions of fog, added that line to my ~/.gemrc, and then reinstalled them. Much better now! Thanks!

fletcher fucked around with this message at 23:44 on Oct 9, 2014

Molten Llama
Sep 20, 2006

necrotic posted:

code:
gem: --no-ri --no-rdoc

Preferably --no-document today unless you're rocking an extremely old version of Rubygems for legacy applications.

--no-ri --no-rdoc has been deprecated since ~2.0 and will (eventually) be going away.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Molten Llama posted:

Preferably --no-document today unless you're rocking an extremely old version of Rubygems for legacy applications.

--no-ri --no-rdoc has been deprecated since ~2.0 and will (eventually) be going away.

I did not know this! Time to update my rc file and change what I tell everyone at work.

Jaded Burnout
Jul 10, 2004


Pollyanna posted:

Is there a particular convention when generating controllers? Way I understand it is that the basic format is rails g controller ControllerName action1 action2. Is that right? And what sorts of things need a controller? It makes sense for a controller to be called User and have actions create and delete, but what would a controller for static pages do? What other kinds of things would necessitate a controller?

Think about it from the perspective of responsibility. Your views are responsible for presenting data in HTML form (or JSON, or..). Your models are responsible for executing actions on data like finding something, saving something, executing a state change, etc. Your controllers are responsible for orchestrating everything to get the user's request served appropriately.

From that point of view, controllers handle the "what"; what a given request means, what to do if they're unauthenticated, what data to return to the user, while your views and models handle the how. How should I display this data, or how do I record the fact that this or that document is published.

If you're following REST, the "what" is pretty well defined by REST itself, so follow the REST convention and you'll be fine. Not all requests are about resources as you've discovered, in which case the scope of your controller is up to you, you have to assign responsibility to that controller appropriately for what it's doing.

In the case of a "static" controller, the "what" is typically just finding the right view to render and providing a few bits of generic data.

As with all things Rails, you don't have to do things the Rails way, but it will fight you every step if you don't.

KoRMaK
Jul 31, 2012



I have a dashboard and some other pages that just have a route setup, and methods in the controller that gather the info needed and then the view is rendered.

The conventions are there to stick to, to keep you on the 'rails', but you can deviate from them whenever, at your own benefit or peril, as you see fit. They are just suggestions for best practices.

EVGA Longoria
Dec 25, 2005

Let's go exploring!

KoRMaK posted:

I have a dashboard and some other pages that just have a route setup, and methods in the controller that gather the info needed and then the view is rendered.

The conventions are there to stick to, to keep you on the 'rails', but you can deviate from them whenever, at your own benefit or peril, as you see fit. They are just suggestions for best practices.

This. MVC is a powerful concept and worth using in a lot of CRUD cases. But it is not he be-all end-all that it looks at time. If your model consists entirely of static queries that just pull a report, it's not unreasonable to put that in a controller and have no model.

Jaded Burnout
Jul 10, 2004


EVGA Longoria posted:

This. MVC is a powerful concept and worth using in a lot of CRUD cases. But it is not he be-all end-all that it looks at time. If your model consists entirely of static queries that just pull a report, it's not unreasonable to put that in a controller and have no model.

You're right, though consideration should be given to testing and reuse. Rails controllers are very muddy, in that they like to mix their internals with views and a bunch of other rails mechanisms, making them harder and slower to unit test than models (activerecord or otherwise).

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
The simplistic rails model also misses the service / actual business logic layer. Don't be a dummy and try to fit everything an app does into rails controllers or models. The dumber they both are (i.e. Controllers only respond to http requests, models pretty much just hold data), the better off your app will be in the long run.

Smol fucked around with this message at 08:07 on Oct 12, 2014

Thalagyrt
Aug 10, 2006

Smol posted:

The simplistic rails model also misses the service / actual business logic layer. Don't be a dummy and try to fit everything an app does into rails controllers or models. The dumber they both are (i.e. Controllers only respond to http requests, models pretty much just hold data), the better off your app will be in the long run.

I wish there were more resources out there that explained this, because it's very true. The Rails model works for simple applications, but once you get beyond simple it falls apart. My models in the vNucleus portal have plenty of logic on them since it's a pretty complex application, but that logic is entirely data consistency/manipulation logic, not business logic. The meat of the application is all factored out into separate classes that encapsulate that operation, making extensive use of Wisper for services that controllers consume, ActiveModel::Model based form objects for any complex action that takes data in, and plain old service classes with an interface that makes sense for the service for other units of logic. It really does make maintaining the application much easier in the long run! It's also extremely easy to test due to the way the logic has been factored out. I've got about 1500 tests and 96% coverage at present.

Pollyanna
Mar 5, 2005

Milk's on them.


Huh. That answers a lot of my questions, actually. I was always confused by Rails' "models only data, controllers only processing/logic" dealio. I'm trying to make a model output its characteristics in a particular order now, and I'm having trouble figuring out whether to do that in the Model, Controller, or View. I want to say the Model, going by rspec-rails documentation, but apparently not...?

KoRMaK
Jul 31, 2012



I'd put it on the model.

necrotic
Aug 2, 2005
I owe my brother big time for this!

Pollyanna posted:

Huh. That answers a lot of my questions, actually. I was always confused by Rails' "models only data, controllers only processing/logic" dealio. I'm trying to make a model output its characteristics in a particular order now, and I'm having trouble figuring out whether to do that in the Model, Controller, or View. I want to say the Model, going by rspec-rails documentation, but apparently not...?

In terms of output for an API? Use jbuilder views for that.

Dystram
May 30, 2013

by Ralp
I love working with Rails but I feel like a fraud, using gems for everything.

Should I try rolling my own functionality for lots of things rather than just installing a bunch of gems if I ever want to land a Rails dev job?

KoRMaK
Jul 31, 2012



Dystram posted:

I love working with Rails but I feel like a fraud, using gems for everything.

Should I try rolling my own functionality for lots of things rather than just installing a bunch of gems if I ever want to land a Rails dev job?

The opposite: the more gems you know how to use and are familiar with the quicker you can one man army an app. It's the best part of rails, you just pluck libraries you need that would normally take a while in itself to develop, test and maintain.

Doh004
Apr 22, 2007

Mmmmm Donuts...
I'm just slowly working my way through my first rails application and just rolled my own user authentication. I know there's tons of gems out there that do this already though... but it's a good learning experience, right? :ohdear:

Dystram
May 30, 2013

by Ralp

KoRMaK posted:

The opposite: the more gems you know how to use and are familiar with the quicker you can one man army an app. It's the best part of rails, you just pluck libraries you need that would normally take a while in itself to develop, test and maintain.

Good to know. Thanks!

Adbot
ADBOT LOVES YOU

Dystram
May 30, 2013

by Ralp

Doh004 posted:

I'm just slowly working my way through my first rails application and just rolled my own user authentication. I know there's tons of gems out there that do this already though... but it's a good learning experience, right? :ohdear:

Yeah, it's good to know how to do it.

https://www.railstutorial.org/book has a few chapters dedicated to it.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply