Skip to content

Instantly share code, notes, and snippets.

@themoxman
Last active August 29, 2015 13:56
Show Gist options
  • Select an option

  • Save themoxman/9028597 to your computer and use it in GitHub Desktop.

Select an option

Save themoxman/9028597 to your computer and use it in GitHub Desktop.
module NoteHandler
# params = { "new_tags"=>"People, Entrepreneurs ", "tag_list"=>"Ruby, Rails, ActiveRecord, " }
# @note.add_tags(params[:new_tags], params[:tag_list])
def add_tags(existing_tags, new_tags)
tag_names = tag_names(existing_tags, new_tags)
self.tags << tag_creator(tag_names)
end
def tag_names(existing_tags, new_tags)
(existing_tags + new_tags.strip).split(", ")
end
def tag_creator(tag_names)
tag_names.map { |name| collection.tags.find_or_create_by(name: name ) }
end
end
require 'spec_helper'
describe NoteHandler do
let(:collection) { create(:collection) }
let(:note) { create(:note, collection: collection ) }
let(:tag1) { create(:tag, collection: collection) }
let(:tag2) { create(:tag, collection: collection) }
let(:new_tags) {" RSpec, Ruby "}
let(:existing_tags) { "#{tag1.name}, #{tag2.name}, " }
describe "#add_tags" do
it "adds new and existing Tags to the Note" do
note.add_tags(existing_tags, new_tags)
expect(note.tags).to have(4).items
expect(note.tags).to match_array(collection.tags)
end
end
describe "#tag_names" do
it "returns an array of new and existing Tag names" do
expected_tags = (existing_tags + new_tags.strip).split(", ")
actual_tags = note.tag_names(existing_tags, new_tags)
expect(actual_tags).to eq expected_tags
end
end
describe "#tag_creator" do
it "returns existing Tags" do
tags = note.tag_creator([tag1.name, tag2.name])
expect(tags).to match_array(collection.tags)
end
it "creates new Tags" do
tags = note.tag_creator(["Rails", "Ruby"])
expect(tags).to match_array(collection.tags)
end
end
end
# Parameters: {"utf8"=>"✓", "authenticity_token"=>"H6tbVG+9aVXV3NYMztwHQEudsKKZ2fKnJPXI98ow1AE=", "note"=>{"title"=>"d
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment