This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <title>Blog</title> | |
| <%= stylesheet_link_tag :all %> | |
| <%= javascript_include_tag 'jquery-1.4.2.min', 'rails', 'application' %> | |
| <%= csrf_meta_tag %> | |
| </head> | |
| <body> | |
| <div id="header"> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= form_tag search_articles_path, :method => :get do %> | |
| <p> | |
| Search | |
| <%= text_field_tag :keyword, params[:keyword] %> | |
| </p> | |
| <% end %> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Blog::Application.routes.draw do | |
| root :to => "articles#index" | |
| resources :articles do | |
| member do | |
| post :notify_friend | |
| end | |
| collection do | |
| get :search | |
| end | |
| resources :comments |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ArticlesController < ApplicationController | |
| before_filter :authenticate, :except => [:index, :show, :notify_friend, :search] | |
| def search | |
| @articles = Article.search(params[:keyword]) | |
| render :action => 'index' | |
| end | |
| # GET /articles | |
| # GET /articles.xml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'test_helper' | |
| $stdout = StringIO.new | |
| def create_properties_table | |
| ActiveRecord::Schema.define(:version => 1) do | |
| create_table :properties do |t| | |
| t.column :name, :string | |
| t.column :description, :text | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'test_helper' | |
| class SimpleSearchTest < ActiveSupport::TestCase | |
| test "search method is available" do | |
| assert Article.respond_to?(:search) | |
| end | |
| test "should search" do | |
| assert_equal 1, Article.search("framework").size | |
| assert_equal 0, Article.search("unknown").size |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Article < ActiveRecord::Base | |
| validates :title, :presence => true | |
| validates :body, :presence => true | |
| belongs_to :user | |
| has_and_belongs_to_many :categories | |
| has_many :comments | |
| acts_as_taggable | |
| simple_search :title, :body |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| require 'active_record' | |
| require 'simple_search' | |
| ActiveRecord::Base.send(:extend, BeginningRails::SimpleSearch) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module BeginningRails | |
| module SimpleSearch | |
| def simple_search(*fields) | |
| class_inheritable_accessor :searchable_fields | |
| raise "Please specify the fields to search on" if fields.empty? | |
| self.searchable_fields = fields | |
| end | |
| def search(value) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <%= div_for article do %> | |
| <h3> | |
| <%= link_to article.title, article %> | |
| <% if article.owned_by? current_user %> | |
| <span class='actions'> | |
| <%= link_to t('general.edit'), edit_article_path(article) %> | |
| <%= link_to t('general.delete'), article, :confirm => t('general.are_you_sure'), :method => :delete %> | |
| </span> | |
| <% end %> | |
| </h3> |