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
Mrs. Wynand
Nov 23, 2002

DLT 4EVA

Operation Atlas posted:

With couchdb you don't really need migrations- if you do it right you can keep older objects in the older schema and only update the database record when necessary, and when they get saved they get saved in the new schema.

Kind of hard to explain unless you've done an ORM that does that, but migrations truly aren't necessary if you plan ahead correctly.

When you do a query (i.e. make a view), don't you have to take both 'schemas' into account? Won't that get unwieldily after a while?

Adbot
ADBOT LOVES YOU

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Mr. Wynand posted:

When you do a query (i.e. make a view), don't you have to take both 'schemas' into account? Won't that get unwieldily after a while?

Queries are done by class attribute, so no. Full-text search is handled by Lucene, which doesn't care either.

The only exception are reports, of which I've had to do one on the couchdb side (the rest I've been able to fetch a bunch of data and process it in ruby), and are pretty easy to update and add an "if" statement, or to go ahead and run a "migration" which just goes through all the records and saves them, therefore updating them to the new schema.

CHRISTS FOR SALE
Jan 14, 2005

"fuck you and die"
I've been hard at work developing a plugin for Rails that generates a uniform, standards-compliant and easy to navigate administration interface for all models in a given app. It will feature its own single-user authentication system bundled (probably SuperSimpleAuthentication), but will also feature hooks to other popular multiuser solutions like restful_authentication and Authlogic (requires an extra flag upon generation and a migration run).

The plugin is called rootkit and it's meant to make developing a backend, an area of the site where a limited amount of people will be viewing, easier so more care can be taken for the high-traffic frontend. All too often I find myself tediously coding an admin interface for a site because inline administration would just make things incredibly confusing. The only real difference is modifying how it authenticates users for entry into the admin section. This idea was borrowed from Django, which includes a built-in secure authorization system as well as live administration control panel which automatically adds new models you've created.

I've created the bare bones of what I want it to do. There are a couple meta-programming things that I honestly don't know how to do (like finding all Model classes for the given application), but I'm going to be delving deeper into Ruby to figure poo poo out. If anyone out there would like to contribute to this project, the repo is located at http://github.com/tubbo/rootkit.

If you would like to contribute, message me through github or email tubbo (at) psychedeli (dot) ca

keep it down up there!
Jun 22, 2006

How's it goin' eh?

Quick Question.

I went to install InstantRails for Windows development. The only legit once I've been able to find is here at RubyForge:
http://rubyforge.org/projects/instantrails/

But it says it was last updated in Dec 2007. That doesn't seem right to me. Is there another place I can get it, or is this actually the latest version?

Or should I go through the process in this guide instead?
http://blog.mmediasys.com/2009/07/06/getting-started-with-rails-and-mysql/


And before anyone mentions it, I wont be using another OS so please don't bring that up.

Thanks

NotShadowStar
Sep 20, 2000
Yeah Instant Rails has been stagnant for years and is probably dead. That link looks pretty comprehensive, try to follow it exactly and you should be okay. If you get stuck feel free to ask, we'll try and help.

keep it down up there!
Jun 22, 2006

How's it goin' eh?

NotShadowStar posted:

Yeah Instant Rails has been stagnant for years and is probably dead. That link looks pretty comprehensive, try to follow it exactly and you should be okay. If you get stuck feel free to ask, we'll try and help.

Thanks, I'll do that.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

CHRISTS FOR SALE posted:

:words:

I'm developing a similar kind of thing for CouchDB. I'll let you know when it is ready to be released.

GroceryBagHead
Nov 28, 2000

CHRISTS FOR SALE posted:

:words:

It's a great idea, especially in early stages of dev / beta releases you can live with a really basic admin views. It really doesn't take very long to implement, but if there was an option to skip it...

Few things though:
1. Why not use it as an Engine? I don't see why you need to generate craploads of views and controllers in the app itself. Let user override defaults if he wishes though.
2. @models = Model.all(:table => RAILS_APP) How does that even work? I checked documentation just to be sure, but there's nothing like this there. Plus, wouldn't you just want AR models?
3. Tests. If there are no tests - it doesn't work.

Lobster Johnson
Sep 23, 2004

jealous of the flies and the worms inside me

BUGS OF SPRING posted:

I went to install InstantRails for Windows development.

If you don't have any luck with that guide, perhaps give JRuby a whirl. Rails' script/server works for development, or there's the the glassfish gem. I've got it running with minimal fuss on OSX and I've heard positive things about its use on Windows.

plasticbugs
Dec 13, 2006

Special Batman and Robin
Can someone please help me understand why my code is so unwieldy? I've been teaching myself, but it looks like I'm missing some remedial Ruby concepts.

I'm writing a browser based game/following a tutorial as another learning exercise and have a simple combat system set up. My fight(monster) method returns an Array called @combat.

I'm trying to customize my model so the browser receives different messages depending on some of the equations inside the fight(monster) method.

It works, but what I've done can't possibly be the best way to go about spitting out messages from a method that returns an array that 'should' be able to store a bunch of additional strings for me.

Here's some of the code in my Player model along with questions (as comments):

code:
# The first method returns the first message I want to convey if the player is struck first by a monster.
# The sentence cuts short because the View returns the name of the monster. Shouldn't this 'message'
# method return the monster's name instead of having the view do it? And how would I go about doing
# that? I'd need to make an instance of Monster (which is stored in the session) available to this message
# method which exists inside a different class.
 
  def message
    if @firstAttack > 95
      "You were surprised by a "
    end
  end  
  
# What if I wanted to also say in another message something like "You got lucky and hit for XXX damage".
# What's the best way to send that message to the browser without writing what seems like another
# one-time-use method such as "message2" based on another instance variable pulled from the
# fight(monster) method. Like, should I be declaring 'damage' as another instance variable and returning a
# separate string like "You got lucky" by checking the value of 'damage inside another method? That seems 
# very wrong to me.

# Here's the fight(monster) method just for completeness.

   def fight(monster)
     combat = Array.new

# This gives the player a 95 percent chance of attacking first
      @firstAttack = 1 + rand(100)

     if @firstAttack < 95
       turn_number = 0
     else
       turn_number = 1
     end

     while (self.alive? and monster.alive?)
       turn_number += 1

       # switch the roles of attacker and defender back and forth

       if (turn_number % 2 != 0)
         attacker = monster
         defender = self
       else
         attacker = self
         defender = monster
       end
       
       y = 1 + rand(5)
       x = 1 + rand(10)/y

        damage = attacker.attack - defender.defense + x
        if damage < 1
          damage = attacker.attack
        end
        
        if damage > defender.cur_hp
          damage = defender.cur_hp
        end

         defender.cur_hp -= damage

         # We'll only make a turn record for those cases where something actually happens
         turn = Hash.new
         turn["attacker"] = attacker.name
         turn["defender"] = defender.name
         turn["damage"] = damage

         #save this turn in the combat transcript
         combat << turn
     end
     
     if (self.alive?)
         # You lived and earned gamer points
         self.gamer_points += monster.gamer_points
       else
         # There are no penalties for losing at the moment
         
         end

       # We only save the user's record back to the database. The monster needs to stay
       # unchanged in the database no matter how much damage we did to it so the next one
       # we encounter will still be pristine

       self.save

       # Return the results of combat
       combat
     end
  

plasticbugs fucked around with this message at 11:03 on Dec 2, 2009

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

plasticbugs posted:

Can someone please help me understand why my code is so unwieldy? I've been teaching myself, but it looks like I'm missing some remedial Ruby concepts.

I think you need classes for the entire Fight and the Turns. They don't need to be ActiveRecord classes, just stuff the vanilla classes in lib. Then what you'd do is create a new Fight object, assign the attacker and defender, and then do @fight.commence (or whatever), and that method would handle the creation of turns and the storing of results in an instance variable in the @fight object (you don't have to return anything).

Pardot
Jul 25, 2001




plasticbugs posted:

code

I'm not happy with the perform_turn, but here's a shot.

code:
class Combat
  attr_accessor :turns
  
  def initialize(player, monster)
    @player  = player
    @monster = monster
    @turns = []
    fight!
  end
  
  def fight!
    decide_attacker_defender
    while continue_combat?
      perform_turn
      award_points if @palyer.alive?
      swap_attacker_defender
    end
  end
  
  def decide_attacker_defender
    @attacker = @player
    @defender = @monster
    swap_attacker_defender if suprise_attack?
  end
  
  def swap_attacker_defender
    @attacker, @defender = @defender, @attacker
  end
  
  def suprise_attack?
    @suprise_attack ||= rand(100) > 94
  end
  
  def continue_combat?
    @player.alive? && @monster.alive?
  end
  
  def perform_turn
    y = 1 + rand(5)
    x = 1 + rand(10)/y
    
    damage = @attacker.attack - @defender.defense + x
    
    if damage < 1
      damage = @attacker.attack
    end
    
    if damage > @defender.cur_hp
      damage = @defender.cur_hp
    end
    
    @defender.cur_hp -= damage
  
    @turns << { :attacker => @attacker.name,
                :defender => @defender.name,
                :damage   => damage
              }
  end
  
  def award_player_points
    @player.gamer_points += @monster.gamer_points
  end
  
  def monster_name
    @monster.name
  end

end
 
class Player < ActiveRecord::Base
  def fight(monster) # original method
    @combat = Combat.new(self, monster)
    self.save
    @combat.turns
  end
  
  def message
    "You were surprised by a #{@combat.monster_name}" if @combat.suprise_attack?
  end
end
Have you taken a look at rspec? The whole BDD process of write test, do the smallest thing to get it to pass, write another test, would likely guide you to something like this. Though I didn't test drive this refactoring, I'm afraid.

Pardot fucked around with this message at 01:11 on Dec 3, 2009

plasticbugs
Dec 13, 2006

Special Batman and Robin

Pardot posted:

I'm not happy with the perform_turn, but here's a shot.

code:
class combat(player, monster)
  attr_accessor :turns
  
  def initialize
    @player  = player
    @monster = monster
    @turns = []
    fight!
  end
 ...
Have you taken a look at rspec? The whole BDD process of write test, do the smallest thing to get it to pass, write another test, would likely guide you to something like this. Though I didn't test drive this refactoring, I'm afraid.

This is an enormous help. Thank you for your time. I'm taking your advice and will be looking into Rspec and Cucumber this weekend. Is Cucumber the best way to go about BDD? I know enough Ruby/Rails to be exceedingly dangerous, so is it too early for me to start learning how to write tests? Or is writing tests an integral part of learning (not just Rails but intelligent programming)?

EDIT: typo

plasticbugs fucked around with this message at 21:39 on Dec 2, 2009

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

plasticbugs posted:

is it too early for me to start learning how to write tests? Or is writing tests an integral part of learning (not just Rails but intelligent programming)?

It is never too early to start writing tests. Learning rails without testing is kind of a guessing game and what works seems to be magic or an incomprehensible collection of mystery settings. Using rspec (or another testing framework) will take the guesswork out and will let you know exactly what broke when so that you can fix things more quickly, more easily, and with less magic.

plasticbugs
Dec 13, 2006

Special Batman and Robin

Operation Atlas posted:

It is never too early to start writing tests. Learning rails without testing is kind of a guessing game and what works seems to be magic or an incomprehensible collection of mystery settings. Using rspec (or another testing framework) will take the guesswork out and will let you know exactly what broke when so that you can fix things more quickly, more easily, and with less magic.

Thanks. One more question. Any advice on first steps? I see that Pragprog has an upcoming book on the subject. Until it comes out, is there another book you recommend?

Trabisnikof
Dec 24, 2005

Operation Atlas posted:

It is never too early to start writing tests. Learning rails without testing is kind of a guessing game and what works seems to be magic or an incomprehensible collection of mystery settings. Using rspec (or another testing framework) will take the guesswork out and will let you know exactly what broke when so that you can fix things more quickly, more easily, and with less magic.

Just saying, although I agree testing is awesome, debugging rails isn't magical at all without it.

Anveo
Mar 23, 2002

plasticbugs posted:

Thanks. One more question. Any advice on first steps? I see that Pragprog has an upcoming book on the subject. Until it comes out, is there another book you recommend?

The book is basically finished. Just buy the beta version. I found it quite good.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Trabisnikof posted:

Just saying, although I agree testing is awesome, debugging rails isn't magical at all without it.

When you're first starting it sure can seem like it.

CHRISTS FOR SALE
Jan 14, 2005

"fuck you and die"

GroceryBagHead posted:

1. Why not use it as an Engine? I don't see why you need to generate craploads of views and controllers in the app itself. Let user override defaults if he wishes though.
I'll look into it. What I was trying to accomplish was something that would be easy to implement, a 3-step process that doesn't REQUIRE any dependent plugins.

GroceryBagHead posted:

2. @models = Model.all(:table => RAILS_APP) How does that even work? I checked documentation just to be sure, but there's nothing like this there. Plus, wouldn't you just want AR models?
It doesn't. This is pseudocode that I forgot to comment. I'm actually trying to figure out how to do this. :)

GroceryBagHead posted:

3. Tests. If there are no tests - it doesn't work.
Definitely. These are planned.

GroceryBagHead
Nov 28, 2000

CHRISTS FOR SALE posted:

I'll look into it. What I was trying to accomplish was something that would be easy to implement, a 3-step process that doesn't REQUIRE any dependent plugins.

Maybe you're thinking about old 3rd party Engines. Rails 2.3 has that functionality built in: http://railscasts.com/episodes/149-rails-engines

So instead of your 3-step process there's only one step: install gem.

phazer
May 14, 2003

chirp chirp i'm a buffalo

plasticbugs posted:

Thanks. One more question. Any advice on first steps? I see that Pragprog has an upcoming book on the subject. Until it comes out, is there another book you recommend?

Assuming you mean The rSpec Book the beta PDF is a prefectly fine start. I went from 0 knowledge of cucumber/rspec to regularly doing BDD & testing in every project.

stack
Nov 28, 2000
Does Rails not offer any core option for time_select to display hours in a 12 hour format. I googled and this was about the only old good option
http://code.google.com/p/rails-twelve-hour-time-plugin/

Is that what everyone uses?

Sewer Adventure
Aug 25, 2004

stack posted:

Does Rails not offer any core option for time_select to display hours in a 12 hour format. I googled and this was about the only old good option
http://code.google.com/p/rails-twelve-hour-time-plugin/

Is that what everyone uses?

code:
Time.now.strftime "%Y-%m-%d %I:%M:%S %p"

stack
Nov 28, 2000

Sewer Adventure posted:

code:
Time.now.strftime "%Y-%m-%d %I:%M:%S %p"

Thanks but I was asking about form inputs. Specifically time_select and any option or preferred plugin to provide a different set of inputs for time entry than the 24 hour default.

phazer
May 14, 2003

chirp chirp i'm a buffalo

stack posted:

Does Rails not offer any core option for time_select to display hours in a 12 hour format. I googled and this was about the only old good option
http://code.google.com/p/rails-twelve-hour-time-plugin/

Is that what everyone uses?

That's the only thing I've found.

plasticbugs
Dec 13, 2006

Special Batman and Robin

phazer posted:

Assuming you mean The rSpec Book the beta PDF is a prefectly fine start. I went from 0 knowledge of cucumber/rspec to regularly doing BDD & testing in every project.

Yep, I bought it last week and I'm amazed at how much I've been able to grasp so far. The PDF will live on my Sony Reader until the paper version comes out.

I have a question about IDEs and design. Is there a way I can set-up Textmate to show a live preview of my edits in a browser window - like if I'm tweaking CSS? If not, is this something that Coda does with a Rails project?

Pardot
Jul 25, 2001




plasticbugs posted:

I have a question about IDEs and design. Is there a way I can set-up Textmate to show a live preview of my edits in a browser window - like if I'm tweaking CSS? If not, is this something that Coda does with a Rails project?

I don't know about IDE support, but some guys I met at Windy City Rails this september wrote this which seems cool: http://github.com/tilleryj/CSS-Push

I haven't used it myself yet though.

phazer
May 14, 2003

chirp chirp i'm a buffalo
Has anyone else experienced this problem with Restful Authentication & Webrat?

http://rails_security.lighthouseapp.com/projects/15332/tickets/46-cucumber-features-not-passing-on-rails-232-with-webrat-043

(the forums aren't parsing this URL properly, sorry, can't link)

I have Rails 2.3.5, latest Restful Authentication and WebRat 0.5.3

When I run the features, most of them fail due to ra_response_spec.rb not finding "div.notice", but if I use the application manually in the browser, it's all working as expected, with "div.notice" in the source.

I have one feature I wrote so far using webrat to find the text in a flash message, and it fails, too. Even though the text is there when tested in the browser.

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought
Are you logging in and/or setting the appropriate session data before each of these actions?

phazer
May 14, 2003

chirp chirp i'm a buffalo

Operation Atlas posted:

Are you logging in and/or setting the appropriate session data before each of these actions?

Admittedly, I assume the features that come with Restful Authentication are written correctly and should pass out of the box. Googling my problem, I've found other people who have issues with webrat, but have yet to find a solution. I'm also open to any advice on how to figure out a solution.

The features fail in the same way with webrat commented out :/

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

phazer posted:

Admittedly, I assume the features that come with Restful Authentication are written correctly and should pass out of the box. Googling my problem, I've found other people who have issues with webrat, but have yet to find a solution. I'm also open to any advice on how to figure out a solution.

The features fail in the same way with webrat commented out :/

Admittedly I haven't done any work with webrat, but I'm assuming it isn't any different than selenium etc.

Things should and will fail with Restful Authentication unless you actually log in first. How else is it going to know what user you're supposed to be logged in as? How else are you going to test that authentication is working properly?

phazer
May 14, 2003

chirp chirp i'm a buffalo

Operation Atlas posted:

Admittedly I haven't done any work with webrat, but I'm assuming it isn't any different than selenium etc.

Things should and will fail with Restful Authentication unless you actually log in first. How else is it going to know what user you're supposed to be logged in as? How else are you going to test that authentication is working properly?

I guess what I meant is I assume the features that come with Restful Auth are written to do what you describe. Isn't it bad practice to release/commit code with failing tests? Shouldn't I be able to assume RA comes with passing tests?

Here's an example of a failing scenario from RA default installation:
code:
 Scenario: Anonymous user can log in
    Given an anonymous user
     And  an activated user named 'reggie'
    When  she creates a singular sessions with login: 'reggie', password: 'monkey', remember me: ''
    Then  she should be redirected to the home page
    When  she follows that redirect!
    Then  she should see a notice message 'Logged in successfully'
     And  reggie should be logged in
     And  she should not have an auth_token cookie
And here's the result when the test is run:
code:
Scenario: Anonymous user can log in                                                              
# features/sessions.feature:23
    Given an anonymous user                                                                        
# features/step_definitions/user_steps.rb:8
    And an activated user named 'reggie'                                                           
# features/step_definitions/user_steps.rb:16
    When she creates a singular sessions with login: 'reggie', password: 'monkey', remember me: '' 
# features/step_definitions/ra_navigation_steps.rb:19
    Then she should be redirected to the home page                                                 
# features/step_definitions/ra_response_steps.rb:19
    When she follows that redirect!                                                                
# features/step_definitions/ra_navigation_steps.rb:46
    Then she should see a notice message 'Logged in successfully'                                  

##   v  Error here v


# features/step_definitions/ra_response_steps.rb:113
      Expected at least 1 element matching "div.notice", found 0.
      <false> is not true. (Spec::Expectations::ExpectationNotMetError)
      ./features/step_definitions/ra_response_steps.rb:114:in `/^she should +see an? (\w+) message '([\w !\']+)'$/'
      features/sessions.feature:29:in `Then she should see a notice message 'Logged in successfully'
Here's the source in the browser:
code:
<div class="notice">Logged in successfully</div>

phazer
May 14, 2003

chirp chirp i'm a buffalo
Follow up

Webrat 0.6.0 removed "css_search" from Webrat::XML

Freezing webrat-0.5.3 solved my "Then I should see regexp" problem.

NotShadowStar
Sep 20, 2000
Might want to check out Capybara which is intended to be a drop-in replacement for Webrat in Cucumber and tests at the Rack level, with fallback to Selenium if you need to.

Pardot
Jul 25, 2001




Hey, CHRISTS FOR SALE in some other thread you mentioned you were using macruby? How is it?

Also anyone who as built more than the hello world I did, what do you think of macruby?

Flobbster
Feb 17, 2005

"Cadet Kirk, after the way you cheated on the Kobayashi Maru test I oughta punch you in tha face!"
I designed a small website for my dad using Rails last year. A few pages, some nifty admin stuff for him to log in and upload images to his gallery, edit his links, that kind of thing. The only third-party gem I'm using is RMagick to create image thumbnails, IIRC.

It's being hosted on a shared hosting package (one of the hosting threads in SA Mart) and occasionally the Mongrel just dies unexpectedly. There's nothing useful at all in the logs except for the message that SIGTERM was sent to the process:
code:
** TERM signal received.
** Daemonized, any open files are closed.  Look at log/mongrel.pid and log/mongrel.log for info.
This has happened maybe 10 or so times in the past year since the website has been up, and we don't notice that it's happened until we try to connect to the site, get a 500, and then have to go into cPanel and manually restart the app.

Does anybody have any suggestions for obvious things that I can keep an eye out for? Is there any way to get better diagnostics when this happens, or at the very least, force Mongrel to restart automatically? I don't have SSH access to the host (yeah, I know, but the draw was that it was cheap for my dad), so my options may be a little limited.

Ghotli
Dec 31, 2005

what am art?
art am what?
what.
I have a few webservices that publish rss information to myself for this or that. One is a small rails app and the others are small rack apps. If you're not expecting a ton of traffic or you don't require much database space then heroku is the right option. Their lowest tier service is free and works just fine for personal websites/apps.

http://heroku.com/pricing#blossom-1

Operation Atlas
Dec 17, 2003

Bliss Can Be Bought

Flobbster posted:

Does anybody have any suggestions for obvious things that I can keep an eye out for? Is there any way to get better diagnostics when this happens, or at the very least, force Mongrel to restart automatically? I don't have SSH access to the host (yeah, I know, but the draw was that it was cheap for my dad), so my options may be a little limited.

If you're using an old version of RMagick, there's a memory leak in it. Once the mongrel process gets too big the OS will kill it. That's my #1 guess as to what's going on.

First, upgrade to the latest RMagick/ImageMagick combo. Second (if possible) upgrade to Nginx/Passenger. Way better than mongrel.

Randuin
Dec 26, 2003

O-Overdrive~
I just have a question since the new thing seems to be running passenger/nginx. Is there any reason to use that combo during development at all?

Adbot
ADBOT LOVES YOU

Pardot
Jul 25, 2001




Randuin posted:

I just have a question since the new thing seems to be running passenger/nginx. Is there any reason to use that combo during development at all?

Some people really like it, using some preference pane thing. However everyone who's told me that they like it are contractors who are switching between apps often. I'm just building a single app, so I stick with script/server, and that's been fine.

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