#87 new
Jesse Crockett

Limit daily connection requests

Reported by Jesse Crockett | June 11th, 2008 @ 08:19 PM | in Miscellaneous polish

I think I'm half way there with the following. Please help fill in the blanks for the model and controller. Admin views are finished.

class LimitDailyConnectionRequests < ActiveRecord::Migration
  def self.up
    add_column "connections", "can_request_connection",    :boolean, 
                                                           :default => true
    add_column "connections", "connection_requests_today", :integer
    add_column "preferences", "limit_requests", :boolean,  :default => false
    add_column "preferences", "daily_requests", :integer,  :default => 20
  end

  def self.down
  end
end

Comments and changes to this ticket

  • Jesse Crockett

    Jesse Crockett June 16th, 2008 @ 09:00 AM

    Hello team,

    I need help. I've been fidgeting for days with this code, asking in forums for help, yet I am at a loss. I believe that getting help here with this problem will unlock a somewhat deeper understanding of Rails and Insoshi.

    Here is my code from app/controllers/connections_controller.rb

    The main error I get is "RecordNotFound... Couldn't find Connection without an ID"

    
    
     def can_request?
        @request = Connection.find(params[:id])
        @request.can_request_connection or
               @request.requests_today == Preference.find(params[:daily_requests])
      end
      
      def update_requests
        @requests = Connection.find(params[:id])
        @requests.requests_today += 1
      end
      
      def create
        @contact = Person.find(params[:person_id])
    
        respond_to do |format|
          if Connection.request(current_person, @contact) && can_request?
            update_requests
            flash[:notice] = 'Connection request sent!'
            format.html { redirect_to(home_url) }
          else
            # This should only happen when people do something funky
            # like friending themselves.
            flash[:notice] = "Invalid connection or too many requests"
            format.html { redirect_to(home_url) }
          end
        end
      end
    

    db/migrate/026_limit_daily_connection_requests.rb

    class LimitDailyConnections < ActiveRecord::Migration
      def self.up
    
        add_column "preferences", "limit_requests", :boolean,  :default => false
        add_column "preferences", "daily_requests", :integer,  :default => 20
        add_column "connections", "can_request_connection",    :boolean, 
                                                               :default => true
        add_column "connections", "requests_today", :integer    
      end
    
      def self.down
      end
    end
    

    Please help. I want to become a productive contributor here. Thank you.

  • Jesse Crockett

    Jesse Crockett June 16th, 2008 @ 01:15 PM

    OK, I've got too much to learn for this. It won't hurt my pride to see someone implement this (imo, necessary) enhancement. I'd love to come back and see how it works out in the model & controller, where I'm lost.

    So, off to lynda.com with Fido. See you later.

    #
    # basic migration (still requires column(s) for daily maintenance
    #
    
    class LimitDailyConnections < ActiveRecord::Migration
      def self.up
    
        add_column "preferences", "limit_requests", :boolean,  :default => false
        add_column "preferences", "daily_requests", :integer,  :default => 20
        add_column "connections", "can_request_connection",    :boolean, 
                                                               :default => true
        add_column "connections", "requests_today", :integer    
      end
    
      def self.down
      end
    end
    
    
    #
    # Admin show
    #
    
    <p>
      <b>Email verifications:</b>
      <%=h @preferences.email_verifications %>
    </p>
    
    <p>
      <b>Limit daily connections requests:</b>
      <%=h @preferences.limit_requests %>
    </p>
    
    <p>
      <b>Demo site:</b>
      <%=h @preferences.demo %>
    </p>
    
    
    #
    # Admin edit
    #
    
        <p>Verify email for new registrations using an activation code</p>  
        <div class="form_row">
          <label for="email_verifications"
                 class="checkbox">Email verifications</label>
          <%= f.check_box :email_verifications %>
        </div>
    
        <br />
        
        <p>Limit daily connection requests</p>          
        <div class="form_row">
          <label for="limit_requests"
                 class="checkbox">Limit requests</label>
          <%= f.check_box  :limit_requests %>
          <label for="daily_requests"
                 class="textfield"></label>
          <%= f.text_field :daily_requests %>
        </div>
        
        <br />
        
        <div class="form_row">
          <label for="about">About</label>
          <br />
          <p><%= formatting_note %></p>
    
          <%= f.text_area :about, :rows => 10 %>
        </div>
    
  • Michael Hartl

    Michael Hartl June 17th, 2008 @ 04:40 PM

    • → State changed from “new” to “invalid”
    • → Milestone changed from “” to “Miscellaneous polish”
  • Michael Hartl

    Michael Hartl June 17th, 2008 @ 04:41 PM

    This enhancement should be possible without changing the data model.

  • Michael Hartl

    Michael Hartl June 17th, 2008 @ 04:54 PM

    • → State changed from “invalid” to “new”
  • Jesse Crockett

    Jesse Crockett August 10th, 2008 @ 08:29 AM

    • → Tag changed from “” to “enhancement”

    Hello!

    I've been working on this for a few hours, have integrated the functionality with only two changes to the user model that seem necessary: Preferences :limit_requests (boolean) and :daily_request_limit (integer) via migration.

    With some snappy code in the connections controller:

    @@@ Ruby if can_request? and Connection.request(current_person, @contact)

    . . .

    else

        # This should only happen when people exceed daily request limit
        # or something funky like friending themselves.
        flash[:notice] = "Not sent: invalid connection or exceeds daily \
              request limit (#{Preference.find(:first).daily_request_limit})."
    

    . . .

    # validate against admin daily connection request limit def can_request?

    @requests_limited = Preference.find(:first).limit_requests
    @limit = Preference.find(:first).daily_request_limit
    if @requests_limited
      @list = Connection.find_all_by_person_id_and_status(current_person,
                          Connection::PENDING, :order => 'created_at desc')
      if @limit == 0
        false
      elsif not @list[@limit - 1].nil?
        @list[@limit - 1].created_at < 1.day.ago
      else
       true
      end
    end
    

    end

    
    
    Unfortunately, the examples at the rspec site are perplexing.  It's worth learning, but for now I want to see if the migration is okay.  Otherwise I have no idea how to implement this feature.
    
    Enhancement of the Connection.find_all use is welcome, if it's slow.
    
    Works great!
    
  • Jesse Crockett

    Jesse Crockett August 11th, 2008 @ 10:18 AM

    Finally got this to clear spec. Tested manually down into 1 minute intervals. Works.

    app/models/connection.rb

    
    # Validate against admin daily connection request limit.
        def can_request?(person)
          if Preference.find(:first).limit_requests
            @list = find_all_by_person_id_and_status(person, PENDING,
                                              :order => 'created_at desc')
            @limit = Preference.find(:first).daily_request_limit
            if @limit > 0
              @list[@limit - 1].nil? or @list[@limit - 1].created_at < 1.day.ago
            else
              nil
            end
          else
            true
          end
        end
    
        # Make a pending connection request.
        def request(person, contact, send_mail = nil)
          .
          .
          .
          elsif can_request?(person)
            transaction do
              create(:person => person, :contact => contact, :status => PENDING)
              create(:person => contact, :contact => person, :status => REQUESTED)
            end
            .
            .
            .
        end
    

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

Shared Ticket Bins

People watching this ticket