Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

used virtual attribute to add tagging #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--color
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,9 @@ end

# Use debugger
# gem 'debugger', group: [:development, :test]

group :development, :test do
gem 'rspec-rails'
gem 'capybara'
gem 'launchy'
end
30 changes: 30 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,24 @@ GEM
multi_json (~> 1.3)
thread_safe (~> 0.1)
tzinfo (~> 0.3.37)
addressable (2.3.6)
arel (4.0.2)
atomic (1.1.16)
builder (3.1.4)
capybara (2.2.1)
mime-types (>= 1.16)
nokogiri (>= 1.3.3)
rack (>= 1.0.0)
rack-test (>= 0.5.4)
xpath (~> 2.0)
coffee-rails (4.0.1)
coffee-script (>= 2.2.0)
railties (>= 4.0.0, < 5.0)
coffee-script (2.2.0)
coffee-script-source
execjs
coffee-script-source (1.7.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.0.2)
hike (1.2.3)
Expand All @@ -46,12 +54,17 @@ GEM
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.8.1)
launchy (2.4.2)
addressable (~> 2.3)
mail (2.5.4)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.25.1)
mini_portile (0.5.3)
minitest (4.7.5)
multi_json (1.9.2)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
pg (0.17.1)
polyglot (0.3.4)
rack (1.5.2)
Expand All @@ -73,6 +86,18 @@ GEM
rake (10.2.2)
rdoc (4.1.1)
json (~> 1.4)
rspec-core (2.14.8)
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
rspec-rails (2.14.2)
actionpack (>= 3.0)
activemodel (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
rspec-mocks (~> 2.14.0)
sass (3.2.18)
sass-rails (4.0.2)
railties (>= 4.0.0, < 5.0)
Expand Down Expand Up @@ -104,16 +129,21 @@ GEM
uglifier (2.5.0)
execjs (>= 0.3.0)
json (>= 1.8.0)
xpath (2.0.0)
nokogiri (~> 1.3)

PLATFORMS
ruby

DEPENDENCIES
capybara
coffee-rails (~> 4.0.0)
jbuilder (~> 1.2)
jquery-rails
launchy
pg
rails (= 4.0.4)
rspec-rails
sass-rails (~> 4.0.2)
sdoc
turbolinks
Expand Down
24 changes: 24 additions & 0 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,26 @@
class QuestionsController < ApplicationController

def create
question = Question.create({text: (post_params[:text_field] + 'taglist: ' + post_params[:tag_list] )})
redirect_to question_path(question)
end

def index
@questions = Question.all
end

def new
@question = Question.new
end

def show
@question = Question.find(params[:id])
end

private

def post_params
params.require(:question).permit(:text_field, :tag_list)
end

end
15 changes: 15 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Question < ActiveRecord::Base

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're looking for a setter here, for example I might have the virtual attribute #tag_list= which lets me say...

my_question.tag_list = "foo,bar,baz"

... and it would chop that up and create individual tag objects that it could add to the many-to-many #tags field.

def text_field
if self.text
self.text.split('taglist: ').first
end
end

def tag_list
if self.text
self.text.split("taglist: ").last
end
end

end
5 changes: 5 additions & 0 deletions app/views/questions/_form_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<%= f.label :text_field %>
<%= f.text_area :text_field %>
<%= f.label :tag_list %>
<%= f.text_field :tag_list %>
<%= f.submit "Submit question" %>
6 changes: 6 additions & 0 deletions app/views/questions/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<h1>All Questions</h1>
<ul>
<% @questions.each do |q| %>
<li>question #<%= q.id %>: <%= q.text %></li>
<% end %>
</ul>
4 changes: 4 additions & 0 deletions app/views/questions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<h1>New question</h1>
<%= form_for @question do |f| %>
<%= render "form_fields", f: f %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/questions/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1>post #<%= @question.id %></h1>
<p id="question_text">text: <%= @question.text_field %></p>
<p id="tag_list">tag list: <%= @question.tag_list %></p>
9 changes: 3 additions & 6 deletions config/database.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ development:
encoding: unicode
database: questions_development
pool: 5
username: questions
password:


# Connect on a TCP socket. Omitted by default since the client uses a
# domain socket that doesn't need configuration. Windows does not have
Expand Down Expand Up @@ -48,13 +47,11 @@ test:
encoding: unicode
database: questions_test
pool: 5
username: questions
password:


production:
adapter: postgresql
encoding: unicode
database: questions_production
pool: 5
username: questions
password:

3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Questions::Application.routes.draw do
resources :questions

root to: "questions#index"
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".

Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20140404013928_create_questions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateQuestions < ActiveRecord::Migration
def change
create_table :questions do |t|
t.text :text

t.timestamps
end
end
end
25 changes: 25 additions & 0 deletions db/schema.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# encoding: UTF-8
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140404013928) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

create_table "questions", force: true do |t|
t.text "text"
t.datetime "created_at"
t.datetime "updated_at"
end

end
10 changes: 10 additions & 0 deletions spec/models/question_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
require 'spec_helper'

describe Question do
it 'should have a question' do
q = Question.create(:text => 'this a text fieldtaglist: this is a tag list')
expect(q.text_field).to eq("this a text field")
expect(q.tag_list).to eq("this is a tag list")
end

end
42 changes: 42 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# This file is copied to spec/ when you run 'rails generate rspec:install'
ENV["RAILS_ENV"] ||= 'test'
require File.expand_path("../../config/environment", __FILE__)
require 'rspec/rails'
require 'rspec/autorun'

# Requires supporting ruby files with custom matchers and macros, etc,
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each { |f| require f }

# Checks for pending migrations before tests are run.
# If you are not using ActiveRecord, you can remove this line.
ActiveRecord::Migration.check_pending! if defined?(ActiveRecord::Migration)

RSpec.configure do |config|
# ## Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr

# Remove this line if you're not using ActiveRecord or ActiveRecord fixtures
config.fixture_path = "#{::Rails.root}/spec/fixtures"

# If you're not using ActiveRecord, or you'd prefer not to run each of your
# examples within a transaction, remove the following line or assign false
# instead of true.
config.use_transactional_fixtures = true

# If true, the base class of anonymous controllers will be inferred
# automatically. This will be the default behavior in future versions of
# rspec-rails.
config.infer_base_class_for_anonymous_controllers = false

# Run specs in random order to surface order dependencies. If you find an
# order dependency and want to debug it, you can fix the order by providing
# the seed, which is printed after each run.
# --seed 1234
config.order = "random"
end
11 changes: 11 additions & 0 deletions test/fixtures/questions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

# This model initially had no columns defined. If you add columns to the
# model remove the '{}' from the fixture names and add the columns immediately
# below each fixture, per the syntax in the comments below
#
one: {}
# column: value
#
two: {}
# column: value
7 changes: 7 additions & 0 deletions test/models/question_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class QuestionTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end