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



kayakyakr posted:

I also believe that it falls through (or fails to do so). So if you have success.json on the first line and then success.all on the second line, it'll respond with the json line and not the all line.

Don't quote me on that, it might cause a double render as well.
Man, now I have a situation where a large portion of the response is the same for both js and html, but the last line differs.

Can I do any magic with that or do I have to build a separate function and call it from each success.whatever call?

Adbot
ADBOT LOVES YOU

Oh My Science
Dec 29, 2008
I have started looking for a junior rails dev position and it seems like a lot of them require Angular experience now. Having very little experience with it I'm not sure where to get started here. I have created the basic hello world angular + rails api app but I imagine they want a little more exposure than that. Can a more experienced rails dev tell me what kind of demo app / project I should work on to impress potential employers?

MrDoDo
Jun 27, 2004

You better remember quick before we haul your sweet ass down to the precinct.

Oh My Science posted:

I have started looking for a junior rails dev position and it seems like a lot of them require Angular experience now. Having very little experience with it I'm not sure where to get started here. I have created the basic hello world angular + rails api app but I imagine they want a little more exposure than that. Can a more experienced rails dev tell me what kind of demo app / project I should work on to impress potential employers?

I struggle with this sometimes too, I don't really have a great "creative" mind to break out of the usual sample apps when learning a new framework or language. If anyone else out there has input good things they do to come up decent side apps to work on I would be really interested. I tend to get stuck too much in creating solutions for my job which doesn't help since I can't open source it.

For myself what I have been trying to do is find basic stuff in your life where you might thing a web app could potentially be useful. Even if its something that has been rehashed like a To-Do or note taking app its worth it to just get something out there that is completely your own, and has your own coding style. One interesting thing I found recently is a bunch of open APIs for random bits of city data (http://www.socrata.com/customer-spotlight/). It has stuff like 911 call data, lists of local parks, etc. Especially for JS MVC frameworks I have found this might prove somewhat helpful to act a data source you can just tie in to and build a simple single page app that will break away from the repeated example apps out there.

MrDoDo fucked around with this message at 01:11 on Feb 4, 2014

kayakyakr
Feb 16, 2004

Kayak is true

KoRMaK posted:

Man, now I have a situation where a large portion of the response is the same for both js and html, but the last line differs.

Can I do any magic with that or do I have to build a separate function and call it from each success.whatever call?

you could put the shared code above the response section? a separate function would be good too.

Oh My Science posted:

I have started looking for a junior rails dev position and it seems like a lot of them require Angular experience now. Having very little experience with it I'm not sure where to get started here. I have created the basic hello world angular + rails api app but I imagine they want a little more exposure than that. Can a more experienced rails dev tell me what kind of demo app / project I should work on to impress potential employers?

I hate this. Requiring a front-end framework? These things are very easy to learn, I'd rather hire someone that can program that might not have experience with the framework and train them up than spend time looking for someone with just the right qualifications...

kayakyakr fucked around with this message at 02:51 on Feb 4, 2014

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home

kayakyakr posted:



I hate this. Requiring a front-end framework? These things are very easy to learn, I'd rather hire someone that can program that might not have experience with the framework and train them up than spend time looking for someone with just the right qualifications...

Welcome to the industry.

The Journey Fraternity
Nov 25, 2003



I found this on the ground!

The Milkman posted:

Welcome to the industry.

I can't wait for a year from now when people want Angular devs with 4+ years of experience.

Oh My Science
Dec 29, 2008

kayakyakr posted:

I hate this. Requiring a front-end framework? These things are very easy to learn, I'd rather hire someone that can program that might not have experience with the framework and train them up than spend time looking for someone with just the right qualifications...

Junior positions are few and far between in my city (specifically for rails) which is why they can be picky I guess. I have a really solid foundation in web development (everything from design to coding) and just want to be in a position where I can begin specializing in the area I like best. Oh well, I've met a few people at the local ruby meetup group, that may lead to something eventually.

KoRMaK
Jul 31, 2012



Just do a website with a three model relationship with jquery plugins, bootstrap, modals, ajax and angular.

Pardot
Jul 25, 2001




There is a libyaml CVE. If you're on heroku you can push an empty commit which will rebuild your app, and you'll be patched. If you're on something else, you'll need to update libyaml. There is no new ruby patchlevel or changes to ruby itself.

You can see what version libyaml you have with ruby -ryaml -e 'p YAML.libyaml_version' You want [0, 1, 5].

Pardot fucked around with this message at 13:32 on Feb 4, 2014

KoRMaK
Jul 31, 2012



Ruby code:
success.any(:js, :html) { 
  if success.format.symbol == :js
    Rails.logger.info("\n\n Successful js response \n\n") 
  else
    Rails.logger.info("\n\n Successful whatever else \n\n")  
   end
  }
The above works, the below does not.

Ruby code:
success.any { 
  if success.format.symbol == :js
    Rails.logger.info("\n\n Successful js response \n\n") 
  else
    Rails.logger.info("\n\n Successful whatever else \n\n")  
   end
  }
The format becomes :html for all requests with an unspecific any clause, because why not? Everything is getting treated the same.

Peristalsis
Apr 5, 2004
Move along.
I think this will be an easy one, but I'm confused on how to use module functionality in different places of the app.

I have a Utils module in /lib/app_name_utils.rb. In several controller routines, I have logger = Utils::SingletonLogger.get_logger. Sometimes these assignments work, sometimes they don't. In particular, one tester tells me that roughly half the time he goes to one of the index pages, it crashes.

I've been doing some Googling, and I know I could use
code:
require app_name_utils.rb
and then
code:
include Utils
in every class where I use them, but I thought there was a way just to specify the source module when using it (and my original usage seems to work about half the time, I guess).
I'm also not clear if I have to load/require the module everywhere I use it, or just in one central place (for now, I've added config.autoload_once_paths += %W(#{config.root}/lib) to application.rb).

And I found a statement online that claimed that the name of the file in which the module resides should match the name of the module, which I haven't done. Is that critical?

Peristalsis fucked around with this message at 19:49 on Feb 4, 2014

kayakyakr
Feb 16, 2004

Kayak is true

Peristalsis posted:

I think this will be an easy one, but I'm confused on how to use module functionality in different places of the app.

I have a Utils module in /lib/app_name_utils.rb. In several controller routines, I have logger = Utils::SingletonLogger.get_logger. Sometimes these assignments work, sometimes they don't. In particular, one tester tells me that roughly half the time he goes to one of the index pages, it crashes.

I've been doing some Googling, and I know I could use
code:
require app_name_utils.rb
and then
code:
include Utils
in every class where I use them, but I thought there was a way just to specify the source module when using it (and my original usage seems to work about half the time, I guess).
I'm also not clear if I have to load/require the module everywhere I use it, or just in one central place (for now, I've added config.autoload_once_paths += %W(#{config.root}/lib) to application.rb).

And I found a statement online that claimed that the name of the file in which the module resides should match the name of the module, which I haven't done. Is that critical?

Consider using the newly standardized concerns paradigm?

http://richonrails.com/articles/rails-4-code-concerns-in-active-record-models

Peristalsis
Apr 5, 2004
Move along.

Alas, we aren't using Rails 4, and I don't want to restructure this code that was supposed to go out two months ago any more than necessary.

Edit:
Let me streamline my questions.
1) Is it enough for the application just to require/load the file somewhere before its modules are included, or does it have to be loaded in every file in which it is used?
2) Once loaded, does its module have to be included in every file where it will be used, or is there some global include list?
3) Is the name of the file relative to the name of the module important?
4) Is there a way to access the module's code without including it? (E.g., using ModuleName::MethodName seems to work some of the time in test, and all the time in DEV. I must have found that somewhere....)
5) If I do include the module in a file, can I still specify the module from which it came when I call it, or do I have to call it without a prefix or module specification of any sort? I'd like to keep the module name included when it's used just for clarity.

Peristalsis fucked around with this message at 22:17 on Feb 4, 2014

kayakyakr
Feb 16, 2004

Kayak is true

Peristalsis posted:

Alas, we aren't using Rails 4, and I don't want to restructure this code that was supposed to go out two months ago any more than necessary.

Edit:
Let me streamline my questions.
1) Is it enough for the application just to require/load the file somewhere before its modules are included, or does it have to be loaded in every file in which it is used?
2) Once loaded, does its module have to be included in every file where it will be used, or is there some global include list?
3) Is the name of the file relative to the name of the module important?
4) Is there a way to access the module's code without including it? (E.g., using ModuleName::MethodName seems to work some of the time in test, and all the time in DEV. I must have found that somewhere....)
5) If I do include the module in a file, can I still specify the module from which it came when I call it, or do I have to call it without a prefix or module specification of any sort? I'd like to keep the module name included when it's used just for clarity.

You should be able to require the file once and have it available, but you're probably safer to require it in every file that it's used. If you want it globally available, adding it to the autoload path is the way to go.

Ruby code:
config.autoload_paths += %W(#{config.root}/lib)
config.autoload_paths += Dir["#{config.root}/lib/**/"]
You should have the filename match what's in there. While it isn't entirely required by Ruby, Rails just acts better when the file names match the class names.

"include" and "extend" are modifying the class, adding the methods from the module onto the class that you're including them into. So yes, you have to include the module in every class that you want to have methods from the module. If your module doesn't have methods that you're adding to a class and you are just using it as a namespace, then you don't want to include it at all.

While you could directly access module methods, I would suggest using a class with class methods instead, if you're going to do things that way.

Buckhead
Aug 12, 2005

___ days until the 2010 trade deadline :(
I am new-ish to Rails and web development in general and just managed to move my Rails app with PostgreSQL from Heroku to Digital Cloud using Dokku (which is basically meant to emulate Heroku). It was pretty challenging but all is working now. The one remaining thing I want to accomplish is to connect to my app's PostgreSQL server using pg Admin III on Windows. I have found some instructions that require a Linux tunnel but I don't think that would apply to me. Does anyone have any advice on how to do this?

(P.S. When everything finally started to click with how Rails and jQuery play together I think I fell in love. I never thought I'd be able to make apps as cool as I have been able to do with what I have learned in just the past month.)

vvvvvvvvvv Thanks! That worked.

Buckhead fucked around with this message at 00:02 on Feb 11, 2014

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Buckhead posted:

I am new-ish to Rails and web development in general and just managed to move my Rails app with PostgreSQL from Heroku to Digital Cloud using Dokku (which is basically meant to emulate Heroku). It was pretty challenging but all is working now. The one remaining thing I want to accomplish is to connect to my app's PostgreSQL server using pg Admin III on Windows. I have found some instructions that require a Linux tunnel but I don't think that would apply to me. Does anyone have any advice on how to do this?

Shouldn't you just be able to open up the pg port on your server and connect to it that way (probably a really bad idea for security reasons)?

Anyhow, you should be able to open an SSH tunnel on windows but I've never done it. Try using PuTTY. Here's their instructions for port forwarding:

http://the.earth.li/~sgtatham/putty/0.63/htmldoc/Chapter3.html#using-port-forwarding

What does windows do instead of SSH? I can't imagine functioning without it.

DONT THREAD ON ME fucked around with this message at 23:30 on Feb 10, 2014

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home
I'm working on a thing where an admin can upload a series of images, and the front end will display one, then after X amount of time start displaying the next one. Alternatively it could complete a cycle every X (6-12ish) hours. It's not super strict in that regard. It's not a live loading JS thing, just on page load you'll get an image from the server.

My first thought would be whenever/cron but perhaps there's a better way? Perhaps slicing up a chunk of time by the count of images and comparing it against the current time? I'd appreciate input from anyone who's tackled something like this before.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

The Milkman posted:

I'm working on a thing where an admin can upload a series of images, and the front end will display one, then after X amount of time start displaying the next one. Alternatively it could complete a cycle every X (6-12ish) hours. It's not super strict in that regard. It's not a live loading JS thing, just on page load you'll get an image from the server.

My first thought would be whenever/cron but perhaps there's a better way? Perhaps slicing up a chunk of time by the count of images and comparing it against the current time? I'd appreciate input from anyone who's tackled something like this before.

I'm not really understanding your question. Why can't you just use timeout? If you're not doing liveloading, you can just have an array of image URLs and every x minutes it pops a url off the array and displays it.

Your method of comparing time would work but it would be needlessly complicated unless it would achieve some purpose that I'm not understanding.

Chilled Milk
Jun 22, 2003

No one here is alone,
satellites in every home

USSMICHELLEBACHMAN posted:

I'm not really understanding your question. Why can't you just use timeout? If you're not doing liveloading, you can just have an array of image URLs and every x minutes it pops a url off the array and displays it.

Your method of comparing time would work but it would be needlessly complicated unless it would achieve some purpose that I'm not understanding.

Because I didn't know about timeout :)

Yeah I wasn't crazy about that idea, that's why I asked. Never had to deal with time in this sort of way before, especially in a web application and figured there had to be something better than what I cooked up on a monday with little sleep. Thanks!



Edit: here's what I came up with this morning


initializer:
code:
Thread.new {
  loop do
    sleep 15.seconds # Just for testing
    image = SidebarImage.cycle_image
    puts "Cycling Sidebar Image: ##{image.position}: #{image.url}"
  end
}
in model:
code:
  def self.current
    @@current ||= first
  end

  def self.cycle_image
    if @@current == last
      @@current = first
    else
      @@current = find_by_position(@@current.position + 1)
    end
  end

Chilled Milk fucked around with this message at 19:04 on Feb 11, 2014

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

The Milkman posted:

I'm working on a thing where an admin can upload a series of images, and the front end will display one, then after X amount of time start displaying the next one. Alternatively it could complete a cycle every X (6-12ish) hours. It's not super strict in that regard. It's not a live loading JS thing, just on page load you'll get an image from the server.

Ruby code:
complete_cycle_time = 6.hours
seconds_into_cycle = Time.now.to_i % complete_cycle_time
available_images = Image.where(visible: true).order(id: 'asc')
time_per_image = complete_cycle_time / available_images.length
current_image_index = (seconds_into_cycle / time_per_image).to_i
current_image = available_images[current_image_index]
Probably not correct, but it's a starting point I think?

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
I'd probably just create a sequence in my database, do something like (I forget the AR syntax, haven't been using Rails in a while)

code:
SELECT * FROM images 
  ORDER BY id
  OFFSET (nextval('images_offset_sequence') % (SELECT COUNT(*) FROM images))
  LIMIT 1
and then cache the result for the duration you want to show the same image. This solution obviously won't skip images if nobody's using the system, which might be a good thing or a bad thing. The exact solution depends on that and the order you want to show the images in.

KoRMaK
Jul 31, 2012



I have some code that repeats in all of my controller functions, so I'd like to move it to a module as a method. It uses "super" though, and this is where the thing falls apart. How do I pass the "super" method to be invoked by the helper function, or how do I run the edit_helper inline as a proc or something so that "super" makes sense?

Ruby code:
#module
module MyModule
  def edit_helper
    super do |success|
      success.js { render "my/partial", :locals => { :my_local => some_val } }
    end
  end
end

#some controller

  def edit    
    ....
    edit_helper 
    #super do |success|
    #  success.js { render "my/partial", :locals => { :my_local => some_val } }
    #end
  end  

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
The problem with what you're trying to do is that in order for your module to call the superclass method :edit, your module method needs to be named :edit. If you do that, it will work fine. But the problem then is that you have a module method :edit, and a class method :edit, and they will conflict.

Instead of calling calling the super method edit, can you rename the superclass method to something else and call it directly? or alias it?

DONT THREAD ON ME fucked around with this message at 22:51 on Feb 14, 2014

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
You could also do something like this:

code:

module MyModule
  def edit_helper
	self.class.superclass.class_eval do 
		alias_method :edit_helper, :edit
	end
	super do |success|
      	success.js { render "my/partial", :locals => { :my_local => some_val } }
        end
     end
  end
end

    
#some controller

  def edit    
    ....
    edit_helper 
     ...
  end  

There's probably a better way to do this.

DONT THREAD ON ME fucked around with this message at 23:37 on Feb 14, 2014

Smol
Jun 1, 2011

Stat rosa pristina nomine, nomina nuda tenemus.
That's a pretty terrible way to do it, especially performance-wise (MRI, for example, will invalidate all inline method caches when you alias an method). I'll post my thoughts tomorrow

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder

Smol posted:

That's a pretty terrible way to do it, especially performance-wise (MRI, for example, will invalidate all inline method caches when you alias an method). I'll post my thoughts tomorrow

Yeah I definitely wouldn't do it. I feel like the approach is bad in general, but I don't really have any counter suggestions.

DONT THREAD ON ME fucked around with this message at 08:37 on Feb 15, 2014

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

KoRMaK posted:

I have some code that repeats in all of my controller functions, so I'd like to move it to a module as a method. It uses "super" though, and this is where the thing falls apart. How do I pass the "super" method to be invoked by the helper function, or how do I run the edit_helper inline as a proc or something so that "super" makes sense?

Be less vague: what's the end goal here? What does the repeating code actually do?

Parker Lewis
Jan 4, 2006

Can't Lose


I'm having a brainfart working on this Rails migration.

I'm trying to model an attribute that can have three possible values: "Yes", "No", and "Yes, but..".

What type should I be using for this? A string with a validation to ensure that the value is one of those three things?

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
An integer?

Does the attribute really need to be persisted? i.e. can the state be derived from other attributes on the model?

aunt jenkins
Jan 12, 2001

Sounds like an enum! Rails 4.1.0, which is at beta3 status, includes ActiveRecord::Enum in core. 37signalsBasecamp claims to be running 4.1.0 in production so it can't be total poo poo.

Or if you don't feel like living ON THE EDGE there are a few gems that'll do basically the same thing. simple_enum is one I've used.

double sulk
Jul 2, 2010

aunt jemima posted:

Sounds like an enum! Rails 4.1.0, which is at beta3 status, includes ActiveRecord::Enum in core. 37signalsBasecamp claims to be running 4.1.0 in production so it can't be total poo poo.

Or if you don't feel like living ON THE EDGE there are a few gems that'll do basically the same thing. simple_enum is one I've used.

Never use someone's random gem for stuff like that. You're really just asking for a headache down the line when it's no longer supported because the developer abandoned it. You'd be better off using a simple integer or string value if it's literally only a yes/no/maybe situation if being on an edge version of Rails isn't a possibility. Don't over-engineer a solution that doesn't need to be.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Or switch to MongoDB

Fillerbunny
Jul 25, 2002

so confused.

double sulk posted:

Never use someone's random gem for stuff like that. You're really just asking for a headache down the line when it's no longer supported because the developer abandoned it. You'd be better off using a simple integer or string value if it's literally only a yes/no/maybe situation if being on an edge version of Rails isn't a possibility. Don't over-engineer a solution that doesn't need to be.

This is extremely practical advice, given the situation. Don't change your Rails version or database technology for a single field type.

kayakyakr
Feb 16, 2004

Kayak is true

double sulk posted:

Never use someone's random gem for stuff like that. You're really just asking for a headache down the line when it's no longer supported because the developer abandoned it. You'd be better off using a simple integer or string value if it's literally only a yes/no/maybe situation if being on an edge version of Rails isn't a possibility. Don't over-engineer a solution that doesn't need to be.

Or a boolean where true = yes, false = no, and null = maybe

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
Using null to mean something other than null would probably have a lot of unintended consequences. Plus you wouldn't be able to tell whether the field was truly null or 'maybe.' Just use 1,0,-1. Integers are cheap, and the only part of your domain that needs to have any idea that your db is storing integers and not something meaningful like true/false/maybe are the methods you use to store and access the data. Once you've pulled it from the DB, you can give it a meaningful representation.

Cocoa Crispies
Jul 20, 2001

Vehicular Manslaughter!

Pillbug

USSMICHELLEBACHMAN posted:

Or switch to MongoDB

Then you're demanding a headache down the line.

DONT THREAD ON ME
Oct 1, 2002

by Nyc_Tattoo
Floss Finder
woops

hmm yes
Dec 2, 2000
College Slice
Using null in a boolean to mean anything more than an absence of a value is a bad practice.

Nothing wrong with a string that only allows three values, you can define the values as a constant in the model. This lets you throw the validations down on the constant, populate the form select/radio with the same values, base your tests on them, etc. You probably don't need to go all out and treat the attribute as a state, but you could always upgrade the string attribute to a state if your logic demands it.

kayakyakr
Feb 16, 2004

Kayak is true

USSMICHELLEBACHMAN posted:

Using null to mean something other than null would probably have a lot of unintended consequences. Plus you wouldn't be able to tell whether the field was truly null or 'maybe.' Just use 1,0,-1. Integers are cheap, and the only part of your domain that needs to have any idea that your db is storing integers and not something meaningful like true/false/maybe are the methods you use to store and access the data. Once you've pulled it from the DB, you can give it a meaningful representation.

Unless the absence of value is the default state meaning yes and no haven't been set yet. He was pretty light on details, but if there can only be 3 states (yes, no, unset) then a nullable boolean is fine.

If there are actually 4 states (yes, no, maybe, unset) then yes, an integer or string is more appropriate.

Adbot
ADBOT LOVES YOU

Buckhead
Aug 12, 2005

___ days until the 2010 trade deadline :(

aunt jemima posted:

Sounds like an enum! Rails 4.1.0, which is at beta3 status, includes ActiveRecord::Enum in core. 37signalsBasecamp claims to be running 4.1.0 in production so it can't be total poo poo.

This is pretty cool. Is there a page that lists a summary of new features in Rails 4.1.0?

Also, does anyone have a recommendation for a logging plugin? I'm looking for something that will send a daily summary of any 500 errors with the relevant log output included.

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