Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
[Refactoring] Replace .count and .length with .size for collections
Browse files Browse the repository at this point in the history
Improved program efficiency (in querying in particular) by replacing .count and .length with .size for collections.

Fix error in refactoring

Reverted some .size into .count
  • Loading branch information
Cjay Billones committed Jun 25, 2015
1 parent 5bb04dc commit bb88041
Show file tree
Hide file tree
Showing 47 changed files with 61 additions and 61 deletions.
2 changes: 1 addition & 1 deletion app/controllers/crops_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def new
def edit
@crop = Crop.find(params[:id])

(3 - @crop.scientific_names.length).times do
(3 - @crop.scientific_names.size).times do
@crop.scientific_names.build
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/models/crop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def to_s
end

def default_scientific_name
if scientific_names.count > 0
if scientific_names.size > 0
return scientific_names.first.scientific_name
else
return nil
Expand Down Expand Up @@ -177,7 +177,7 @@ def popular_plant_parts
def interesting?
min_plantings = 3 # needs this many plantings to be interesting
min_photos = 3 # needs this many photos to be interesting
return false unless photos.count >= min_photos
return false unless photos.size >= min_photos
return false unless plantings_count >= min_plantings
return true
end
Expand Down Expand Up @@ -208,7 +208,7 @@ def Crop.interesting
howmany = 12 # max number to find
interesting_crops = Array.new
Crop.randomized.each do |c|
break if interesting_crops.length == howmany
break if interesting_crops.size == howmany
next unless c.interesting?
interesting_crops.push(c)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/member.rb
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def Member.interesting
howmany = 12 # max number to find
interesting_members = Array.new
Member.confirmed.located.recently_signed_in.each do |m|
break if interesting_members.length == howmany
break if interesting_members.size == howmany
if m.interesting?
interesting_members.push(m)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Notification < ActiveRecord::Base
after_create :send_email

def self.unread_count
self.unread.count
self.unread.size
end

def replace_blank_subject
Expand Down
2 changes: 1 addition & 1 deletion app/models/planting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def Planting.interesting(howmany=12, require_photo=true)
seen_owners = Hash.new(false) # keep track of which owners we've seen already

Planting.all.each do |p|
break if interesting_plantings.count == howmany # got enough yet?
break if interesting_plantings.size == howmany # got enough yet?
if require_photo
next unless p.photos.present? # skip those without photos, if required
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def author_date_subject
end

def comment_count
self.comments.count
self.comments.size
end

# return the timestamp of the most recent activity on this post
Expand Down
2 changes: 1 addition & 1 deletion app/models/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def Seed.interesting
interesting_seeds = Array.new

Seed.tradable.each do |s|
break if interesting_seeds.length == howmany
break if interesting_seeds.size == howmany
if s.interesting?
interesting_seeds.push(s)
end
Expand Down
2 changes: 1 addition & 1 deletion app/views/account_types/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @account_type do |f|
- if @account_type.errors.any?
#error_explanation
%h2= "#{pluralize(@account_type.errors.count, "error")} prohibited this account_type from being saved:"
%h2= "#{pluralize(@account_type.errors.size, "error")} prohibited this account_type from being saved:"
%ul
- @account_type.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/accounts/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @account do |f|
- if @account.errors.any?
#error_explanation
%h2= "#{pluralize(@account.errors.count, "error")} prohibited this account from being saved:"
%h2= "#{pluralize(@account.errors.size, "error")} prohibited this account from being saved:"
%ul
- @account.errors.full_messages.each do |msg|
%li= msg
Expand Down
4 changes: 2 additions & 2 deletions app/views/admin/orders/search.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
- unless @orders.empty?
%h2
Found
= pluralize(@orders.count, "result")
= pluralize(@orders.size, "result")

%table.table.table-striped
%tr
Expand All @@ -28,7 +28,7 @@
%td
= order.referral_code
%td
- if order.order_items.count > 0
- if order.order_items.size > 0
- order.order_items.each do |o|
= o.quantity
x
Expand Down
2 changes: 1 addition & 1 deletion app/views/alternate_names/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @alternate_name, :html => {:class => 'form-horizontal', :role => "form"} do |f|
- if @alternate_name.errors.any?
#error_explanation
%h2= "#{pluralize(@alternate_name.errors.count, "error")} prohibited this alternate_name from being saved:"
%h2= "#{pluralize(@alternate_name.errors.size, "error")} prohibited this alternate_name from being saved:"
%ul
- @alternate_name.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/comments/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for(@comment, :html => {:class => "form-horizontal", :role => "form"}) do |f|
- if @comment.errors.any?
#error_explanation
%h2= "#{pluralize(@comment.errors.count, "error")} prohibited this comment from being saved:"
%h2= "#{pluralize(@comment.errors.size, "error")} prohibited this comment from being saved:"
%ul
- @comment.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/crops/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @crop, :html => {:class => 'form-horizontal', :role => "form"} do |f|
- if @crop.errors.any?
#error_explanation
%h3= "#{pluralize(@crop.errors.count, "error")} prohibited this crop from being saved:"
%h3= "#{pluralize(@crop.errors.size, "error")} prohibited this crop from being saved:"
%ul
- @crop.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/crops/_popover.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
%p
%small
- if crop.scientific_names.count > 0
- if crop.scientific_names.size > 0
%i
= crop.scientific_names.first.scientific_name
%br/
Expand Down
2 changes: 1 addition & 1 deletion app/views/crops/_thumbnail.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
.cropinfo
.cropname
= link_to crop.name, crop
- if crop.scientific_names.count > 0
- if crop.scientific_names.size > 0
.scientificname
= crop.scientific_names.first.scientific_name
.plantingcount
Expand Down
6 changes: 3 additions & 3 deletions app/views/crops/index.csv.shaper
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ csv.headers *all_headers

if c.scientific_names.any?
csv.cell :default_scientific_name, c.default_scientific_name
csv.cell :scientific_name_count, c.scientific_names.count
csv.cell :scientific_name_count, c.scientific_names.size
end

if c.parent
Expand All @@ -58,8 +58,8 @@ csv.headers *all_headers
end

csv.cell :plantings_count, c.plantings_count || 0
csv.cell :seeds_count, c.seeds.count
csv.cell :harvests_count, c.harvests.count
csv.cell :seeds_count, c.seeds.size
csv.cell :harvests_count, c.harvests.size

# Sunniness

Expand Down
2 changes: 1 addition & 1 deletion app/views/forums/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @forum, :html => { :class => 'form-horizontal', :role => "form" } do |f|
- if @forum.errors.any?
#error_explanation
%h2= "#{pluralize(@forum.errors.count, "error")} prohibited this forum from being saved:"
%h2= "#{pluralize(@forum.errors.size, "error")} prohibited this forum from being saved:"
%ul
- @forum.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/forums/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
- @forums.each do |forum|
%h2= forum
%p
= pluralize(forum.posts.count, "post")
= pluralize(forum.posts.size, "post")
|
=link_to "Visit forum", forum
|
Expand Down
2 changes: 1 addition & 1 deletion app/views/forums/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
Posts
=link_to "Post something", new_post_path(:forum_id => @forum.id), :class => 'btn btn-primary'

- if @forum.posts.count > 0
- if @forum.posts.size > 0
=render :partial => "posts/summary", :locals => { :posts => @forum.posts }
- else
No posts yet.
Expand Down
2 changes: 1 addition & 1 deletion app/views/gardens/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for(@garden, :html => {:class => "form-horizontal", :role => "form"}) do |f|
- if @garden.errors.any?
#error_explanation
%h2= "#{pluralize(@garden.errors.count, "error")} prohibited this garden from being saved:"
%h2= "#{pluralize(@garden.errors.size, "error")} prohibited this garden from being saved:"
%ul
- @garden.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/gardens/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
- else
= render :partial => 'shared/signin_signup', :locals => { :to => 'add a new garden' }

- if @gardens.length > 0
- if @gardens.size > 0

%div.pagination
= page_entries_info @gardens, :model => "gardens"
Expand Down
8 changes: 4 additions & 4 deletions app/views/gardens/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
:growstuff_markdown
#{strip_tags @garden.description}

- if @garden.photos.count > 0 or (can? :edit, @garden and can? :create, Photo)
- if @garden.photos.size > 0 or (can? :edit, @garden and can? :create, Photo)
.row
%h2 Photos

Expand All @@ -47,14 +47,14 @@
%h3
What's planted here?

- if @garden.plantings.current.count > 0
- if @garden.plantings.current.size > 0
- @garden.plantings.current.each do |p|
= render :partial => "plantings/thumbnail", :locals => { :planting => p }
- else
%p
Nothing is currently planted here.

- if @garden.plantings.finished.count > 0
- if @garden.plantings.finished.size > 0
%h3 Previously planted in this garden
- @garden.plantings.finished.each do |p|
= render :partial => "plantings/thumbnail", :locals => { :planting => p }
Expand Down Expand Up @@ -82,7 +82,7 @@
- else
= link_to "#{othergarden}", garden_path(othergarden)

- if @garden.owner.gardens.inactive.count > 0
- if @garden.owner.gardens.inactive.size > 0
%h4= "Inactive gardens"
%ul
- @garden.owner.gardens.inactive.each do |othergarden|
Expand Down
2 changes: 1 addition & 1 deletion app/views/harvests/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for(@harvest, :html => {:class => "form-horizontal", :role => :form}) do |f|
- if @harvest.errors.any?
#error_explanation
%h2= "#{pluralize(@harvest.errors.count, "error")} prohibited this harvest from being saved:"
%h2= "#{pluralize(@harvest.errors.size, "error")} prohibited this harvest from being saved:"
%ul
- @harvest.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/harvests/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
= page_entries_info @harvests, :model => "harvests"
= will_paginate @harvests

- if @harvests.length > 0
- if @harvests.size > 0

%table.table.table-striped
%tr
Expand Down
2 changes: 1 addition & 1 deletion app/views/harvests/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
:growstuff_markdown
#{ @harvest.description != "" ? @harvest.description : "No description given." }

- if @harvest.photos.count > 0 or (can? :edit, @harvest and can? :create, Photo)
- if @harvest.photos.size > 0 or (can? :edit, @harvest and can? :create, Photo)
%h2 Pictures

%ul.thumbnails
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/_seeds.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
%h2= t('.title')

- cache cache_key_for(Seed) do
- if seeds.length > 0
- if seeds.size > 0

%table.table.table-striped
%tr
Expand Down
2 changes: 1 addition & 1 deletion app/views/members/_gardens.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
:growstuff_markdown
#{ strip_tags g.description }

- if g.photos.count > 0 or (can? :edit, g and can? :create, Photo)
- if g.photos.size > 0 or (can? :edit, g and can? :create, Photo)
.row
%h2 Photos

Expand Down
12 changes: 6 additions & 6 deletions app/views/members/_stats.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,34 @@

%ul
%li
- if member.plantings.count > 0
- if member.plantings.size > 0
= link_to pluralize(member.plantings.size, "planting"), plantings_by_owner_path(:owner => member)
- else
0 plantings
%li
- if member.harvests.count > 0
- if member.harvests.size > 0
= link_to pluralize(member.harvests.size, "harvest"), harvests_by_owner_path(:owner => member)
- else
0 harvests
%li
- if member.seeds.count > 0
- if member.seeds.size > 0
= link_to pluralize(member.seeds.size, "seeds"), seeds_by_owner_path(:owner => member)
- else
0 seeds
%li
- if member.posts.count > 0
- if member.posts.size > 0
= link_to pluralize(member.posts.size, "post"), posts_by_author_path(:author => member)
- else
0 posts

%li
- if member.followed.count > 0
- if member.followed.size > 0
= link_to pluralize(member.followed.size, "follow"), member_follows_path(member)
- else
0 following

%li
- if member.followers.count > 0
- if member.followers.size > 0
= link_to pluralize(member.followers.size, "follower"), member_followers_path(member)
- else
0 followers
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifications/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @notification do |f|
- if @notification.errors.any?
#error_explanation
%h2= "#{pluralize(@post.errors.count, "error")} prohibited this message from being sent:"
%h2= "#{pluralize(@post.errors.size, "error")} prohibited this message from being sent:"
%ul
- @notification.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/notifications/index.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
- content_for :title, "Inbox"

- if @notifications.length > 0
- if @notifications.size > 0
%table.table.table-striped
%tr
%th From
Expand Down
2 changes: 1 addition & 1 deletion app/views/orders/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
- else
In progress
%td
- if order.order_items.count > 0
- if order.order_items.size > 0
- order.order_items.each do |o|
= o.quantity
x
Expand Down
2 changes: 1 addition & 1 deletion app/views/orders/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
- if @order.errors.any?
.alert
#error_explanation
%h3= "#{pluralize(@order.errors.count, "error")} stopped you from checking out:"
%h3= "#{pluralize(@order.errors.size, "error")} stopped you from checking out:"
%ul
- @order.errors.full_messages.each do |msg|
%li= msg
Expand Down
2 changes: 1 addition & 1 deletion app/views/photos/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
- if @sets and @current_set
%h2= @sets.key(@current_set)

- if @sets and @sets.length > 0
- if @sets and @sets.size > 0
%p
= form_tag(new_photo_path, :method => :get, :class => 'form-inline') do
= label_tag :set, "Choose a photo album:", :class => 'control-label'
Expand Down
2 changes: 1 addition & 1 deletion app/views/plant_parts/_form.html.haml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
= form_for @plant_part do |f|
- if @plant_part.errors.any?
#error_explanation
%h2= "#{pluralize(@plant_part.errors.count, "error")} prohibited this plant_part from being saved:"
%h2= "#{pluralize(@plant_part.errors.size, "error")} prohibited this plant_part from being saved:"
%ul
- @plant_part.errors.full_messages.each do |msg|
%li= msg
Expand Down
Loading

0 comments on commit bb88041

Please sign in to comment.