Skip to content

Commit

Permalink
Merge pull request #10 from eric-hu/master
Browse files Browse the repository at this point in the history
Add find_last_flag_for and find_all_flags_for methods to Flagger
  • Loading branch information
Eric Berry committed Dec 13, 2012
2 parents d1bafce + 1603ae9 commit 2b1d454
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ Bundler::GemHelper.install_tasks
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

# Allows for 'rake' and 'rake spec' to execute specs. Fixes bug with message:
# undefined method `shellescape' ...
require 'shellwords'

task :default => :spec
10 changes: 10 additions & 0 deletions lib/make_flaggable/flagger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ def flagged?(flaggable, flag = nil)
fetch_flaggings(flaggable, flag).try(:first) ? true : false
end

# Returns the most recently created flag
def find_last_flag_for(flaggable, flag=nil)
find_all_flags_for(flaggable, flag).first
end

# Returns all flags created by the user for a given flag
def find_all_flags_for(flaggable, flag=nil)
fetch_flaggings(flaggable, flag).order('created_at desc')
end

private

def fetch_flaggings(flaggable, flag)
Expand Down
33 changes: 33 additions & 0 deletions spec/lib/make_flaggable_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,39 @@
@flagger.flagged?(@flaggable).should == false
end
end

describe "find_last_flag_for" do
it "should fetch the most recent flag the flagger has made on the flaggable" do
@flagger.flagged?(@flaggable, :favorite).should == false
@flagger.flagged?(@flaggable, :inappropriate).should == false
@flagger.flag!(@flaggable, :favorite)
@flagger.flag!(@flaggable, :inappropriate)
@flagger.find_last_flag_for(@flaggable).flag.should == "inappropriate"
end

it "should return nil when the flagger has made no flags on the flaggable" do
@flagger.flagged?(@flaggable).should == false

@flagger.find_last_flag_for(@flagger).should == nil
end
end

describe "find_all_flags_for" do
it "should fetch all flags the flagger has made on the flaggable" do
@flagger.flagged?(@flaggable, :favorite).should == false
@flagger.flagged?(@flaggable, :inappropriate).should == false
@flagger.flag!(@flaggable, :favorite)
@flagger.flag!(@flaggable, :inappropriate)
flag_types = @flagger.find_all_flags_for(@flaggable).map(&:flag)
flag_types.should include("inappropriate")
flag_types.should include("favorite")
end

it "should return an empty ActiveRecord relation when the flagger has made no flags on the flaggable" do
@flagger.flagged?(@flaggable).should == false
@flagger.find_all_flags_for(@flaggable).should == []
end
end
end

describe "flaggable" do
Expand Down

0 comments on commit 2b1d454

Please sign in to comment.