New and Announcement search. Disabled impression.
This commit is contained in:
1 parent
033667f99b
commit
f9bdb4a327
39 files changed
+222
-1345
No files matched your search
+17
-10
@@ -9,17 +9,24 @@ class Panel::Announcement::FrontEnd::BulletinsController < OrbitWidgetController
|
||||
# GET /bulletins.xml
|
||||
|
||||
def index
|
||||
|
||||
date_now = Time.now
|
||||
if !params[:category_id].blank?
|
||||
@bulletins = Bulletin.all.can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10)
|
||||
@current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
|
||||
elsif !params[:tag_id].blank?
|
||||
@tag = AnnouncementTag.find(params[:tag_id]) rescue nil
|
||||
@tag = AnnouncementTag.where(key: params[:tag_id])[0] unless @tag
|
||||
@bulletins = @tag.bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10)
|
||||
if !params[:search_query].blank?
|
||||
search_cond = {:is_checked=>true,:is_hidden=>false}
|
||||
search_cond.merge!({:bulletin_category_id => "#{params[:category_id]}" }) if !params[:category_id].blank?
|
||||
search = Redis::Search.query("Bulletin", params[:search_query], :conditions =>search_cond)
|
||||
search_result = search.collect{|t| t["id"]}
|
||||
@news_bulletins = Bulletin.all.can_display.any_in(_id:search_result).page( params[:page_main]).per(10)
|
||||
else
|
||||
@bulletins = Bulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10)
|
||||
date_now = Time.now
|
||||
if !params[:category_id].blank?
|
||||
@bulletins = Bulletin.all.can_display.where(:bulletin_category_id => params[:category_id]).any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10)
|
||||
@current_category = BulletinCategory.from_id(params[:category_id]) rescue nil
|
||||
elsif !params[:tag_id].blank?
|
||||
@tag = AnnouncementTag.find(params[:tag_id]) rescue nil
|
||||
@tag = AnnouncementTag.where(key: params[:tag_id])[0] unless @tag
|
||||
@bulletins = @tag.bulletins.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10)
|
||||
else
|
||||
@bulletins = Bulletin.all.can_display.any_of( {deadline: nil,:postdate.lte => date_now} , {:deadline.gte => date_now,:postdate.lte => date_now} ).desc( :is_top, :postdate).page( params[:page_main]).per(10)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
+3
@@ -47,6 +47,9 @@ class Panel::Announcement::Widget::BulletinsController < OrbitWidgetController
|
||||
get_categorys
|
||||
end
|
||||
|
||||
def bulletins_search_block
|
||||
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
|
||||
+25
-2
@@ -1,9 +1,10 @@
|
||||
# encoding: utf-8
|
||||
|
||||
# require "impressionist"
|
||||
class Bulletin
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
include Mongoid::MultiParameterAttributes
|
||||
include Redis::Search
|
||||
include Impressionist::Impressionable
|
||||
|
||||
BelongsToCategory = :bulletin_category
|
||||
@@ -13,7 +14,7 @@ class Bulletin
|
||||
|
||||
scope :searchable,where(:is_checked=>true,:is_hidden=>false,:is_pending=>false)
|
||||
|
||||
is_impressionable :counter_cache => { :column_name => :view_count }
|
||||
# is_impressionable :counter_cache => { :column_name => :view_count }
|
||||
|
||||
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
has_one :subtitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
@@ -62,7 +63,29 @@ class Bulletin
|
||||
after_save :save_bulletin_links
|
||||
after_save :save_bulletin_files
|
||||
|
||||
redis_search_index(:title_field => :s_title,
|
||||
:alias_field =>:s_title_en ,
|
||||
:score_field => :view_count,
|
||||
:condition_fields => [:is_checked,:is_hidden,:bulletin_category_id],
|
||||
:ext_fields => [])
|
||||
# def gen_title_for_search
|
||||
# # [s_title,s_title_en,s_text_en,s_text_zh_tw].join(' ')
|
||||
# end
|
||||
|
||||
def s_title
|
||||
self.title.zh_tw
|
||||
end
|
||||
|
||||
def s_title_en
|
||||
self.title.en
|
||||
end
|
||||
|
||||
# def s_text_en
|
||||
# Nokogiri::HTML(self.text.en).text
|
||||
# end
|
||||
# def s_text_zh_tw
|
||||
# Nokogiri::HTML(self.text.zh_tw).text
|
||||
# end
|
||||
|
||||
# scope :currently_available, lambda { |category, limit|
|
||||
# # limit ||= 5
|
||||
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
<%= form_tag panel_announcement_front_end_bulletins_path, method: :get do %>
|
||||
<p>
|
||||
<%= text_field_tag :search_query, params[:search_query] %>
|
||||
<%= submit_tag "Search", name: nil %>
|
||||
</p>
|
||||
<% end %>
|
||||
@@ -44,6 +44,7 @@ Rails.application.routes.draw do
|
||||
match "reload_bulletins" => "bulletins#reload_bulletins"
|
||||
match "reload_web_links" => "bulletins#reload_web_links"
|
||||
match "bulletins_side_bar" => "bulletins#bulletins_side_bar"
|
||||
match "bulletins_search_block" => "bulletins#bulletins_search_block"
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Vendored
+5
-16
@@ -10,21 +10,10 @@ class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController
|
||||
|
||||
def index
|
||||
if !params[:search_query].blank?
|
||||
search_query =params[:search_query].gsub(/"/,"").split(" ")
|
||||
|
||||
words_query = lambda do |boolean|
|
||||
search_query.each do |word|
|
||||
boolean.should { string "title:#{word}" }
|
||||
#boolean.should { string "sub_title:#{word}" }
|
||||
#boolean.should { string "text:#{word}" }
|
||||
end
|
||||
end
|
||||
|
||||
search_result=Tire.search('news_bulletins') do
|
||||
query {boolean &words_query }
|
||||
#raise to_curl
|
||||
end.results.collect{|t| t.id}
|
||||
|
||||
search_cond = {:is_checked=>true,:is_hidden=>false}
|
||||
search_cond.merge!({:news_bulletin_category_id => "#{params[:category_id]}" }) if !params[:category_id].blank?
|
||||
search = Redis::Search.query("NewsBulletin", params[:search_query], :conditions =>search_cond)
|
||||
search_result = search.collect{|t| t["id"]}
|
||||
@news_bulletins = NewsBulletin.all.can_display.any_in(_id:search_result).page( params[:page_main]).per(10)
|
||||
else
|
||||
date_now = Time.now
|
||||
@@ -50,7 +39,7 @@ class Panel::News::FrontEnd::NewsBulletinsController < OrbitWidgetController
|
||||
else
|
||||
@news_bulletin = NewsBulletin.all.can_display.where(_id: params[:id]).first
|
||||
unless @news_bulletin.disable?
|
||||
impressionist(@news_bulletin)
|
||||
# impressionist(@news_bulletin)
|
||||
get_categorys
|
||||
else
|
||||
render :nothing => true, :status => 403
|
||||
|
||||
Vendored
+3
-2
@@ -47,8 +47,9 @@ caches_page :home_banner
|
||||
get_categorys
|
||||
end
|
||||
|
||||
# def news_bulletins_search_block
|
||||
# end
|
||||
def news_bulletins_search_block
|
||||
|
||||
end
|
||||
|
||||
def home_banner
|
||||
if !params[:category_id].blank?
|
||||
|
||||
+23
-25
@@ -4,6 +4,7 @@ class NewsBulletin
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
include Mongoid::MultiParameterAttributes
|
||||
include Redis::Search
|
||||
include Impressionist::Impressionable
|
||||
|
||||
BelongsToCategory = :news_bulletin_category
|
||||
@@ -16,7 +17,7 @@ class NewsBulletin
|
||||
|
||||
scope :searchable,where(:is_checked=>true,:is_hidden=>false,:is_pending=>false)
|
||||
|
||||
is_impressionable :counter_cache => { :column_name => :view_count }
|
||||
# is_impressionable :counter_cache => { :column_name => :view_count }
|
||||
|
||||
has_one :title, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
has_one :subtitle, :class_name => "I18nVariable", :as => :language_value, :autosave => true, :dependent => :destroy
|
||||
@@ -89,32 +90,29 @@ class NewsBulletin
|
||||
"news_bulletin"
|
||||
end
|
||||
|
||||
def to_indexed_json
|
||||
titles = title.zh_tw #+ title.en
|
||||
sub_titles = subtitle.zh_tw #+ subtitle.en
|
||||
texts = text.zh_tw #+ text.en
|
||||
{
|
||||
:_id => _id,
|
||||
:title => Nokogiri::HTML(titles).text,
|
||||
:sub_title => Nokogiri::HTML(sub_titles).text,
|
||||
:text => Nokogiri::HTML(texts).text
|
||||
}.to_json
|
||||
end
|
||||
redis_search_index(:title_field => :s_title,
|
||||
:alias_field => :s_title_en,
|
||||
:score_field => :view_count,
|
||||
:condition_fields => [:is_checked,:is_hidden,:news_bulletin_category_id],
|
||||
:ext_fields =>[])
|
||||
# def gen_title_for_search
|
||||
# # [s_title,s_title_en,s_text_en,s_text_zh_tw].join(' ')
|
||||
# end
|
||||
|
||||
def self.search( search = nil, category_id = nil )
|
||||
if category_id.to_s.size > 0 and search.to_s.size > 0
|
||||
key = /#{search}/
|
||||
find(:all, :conditions => {title: key, news_bulletin_category_id: category_id}).desc( :is_top, :postdate )
|
||||
elsif category_id.to_s.size > 0 and search.to_s.size < 1
|
||||
find(:all, :conditions => {news_bulletin_category_id: category_id}).desc( :is_top, :postdate )
|
||||
elsif search.to_s.size > 0 and category_id.to_s.size < 1
|
||||
key = /#{search}/
|
||||
find(:all, :conditions => {title: key}).desc( :is_top, :postdate )
|
||||
else
|
||||
find(:all).desc( :is_top, :postdate)
|
||||
end
|
||||
end
|
||||
def s_title
|
||||
#self.title.zh_tw
|
||||
end
|
||||
|
||||
def s_title_en
|
||||
#self.title.en
|
||||
end
|
||||
|
||||
# def s_text_en
|
||||
# Nokogiri::HTML(self.text.en).text
|
||||
# end
|
||||
# def s_text_zh_tw
|
||||
# Nokogiri::HTML(self.text.zh_tw).text
|
||||
# end
|
||||
|
||||
def self.widget_datas( category_id = nil )
|
||||
|
||||
|
||||
+1
@@ -1,6 +1,7 @@
|
||||
<%= form_tag panel_news_front_end_news_bulletins_path, method: :get do %>
|
||||
<p>
|
||||
<%= text_field_tag :search_query, params[:search_query] %>
|
||||
<%= hidden_field_tag :category_id, params[:category_id] %>
|
||||
<%= submit_tag "Search", name: nil %>
|
||||
</p>
|
||||
<% end %>
|
||||
Vendored
+1
-1
@@ -7,7 +7,7 @@ platforms :jruby do
|
||||
end
|
||||
|
||||
platforms :ruby, :mswin, :mingw do
|
||||
# gem 'sqlite3'
|
||||
gem 'sqlite3'
|
||||
end
|
||||
|
||||
gemspec
|
||||
@@ -29,22 +29,17 @@ module Impressionist
|
||||
end
|
||||
|
||||
def impressionist_count(options={})
|
||||
# options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
||||
# imps = options[:start_date].blank? ? impressions : impressions.where("created_at>=? and created_at<=?",options[:start_date],options[:end_date])
|
||||
# options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
|
||||
options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now)
|
||||
imps = options[:start_date].blank? ? impressions : impressions.where(:created_at.gte => options[:start_date], :created_at.lte => options[:end_date])
|
||||
options[:filter] == :all ? imps.count : imps.distinct(options[:filter]).count
|
||||
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
||||
imps = options[:start_date].blank? ? impressions : impressions.where("created_at>=? and created_at<=?",options[:start_date],options[:end_date])
|
||||
options[:filter] == :all ? imps.count : imps.count(options[:filter], :distinct => true)
|
||||
end
|
||||
|
||||
def update_impressionist_counter_cache
|
||||
# cache_options = self.class.impressionist_counter_cache_options
|
||||
# column_name = cache_options[:column_name].to_sym
|
||||
# count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count
|
||||
# update_attribute(column_name, count)
|
||||
cache_options = self.class.impressionist_counter_cache_options
|
||||
column_name = cache_options[:column_name].to_sym
|
||||
update_attribute(column_name, self.send(cache_options[:column_name]) + 1)
|
||||
count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count
|
||||
old_count = send(column_name) || 0
|
||||
self.class.update_counters(id, column_name => (count - old_count))
|
||||
end
|
||||
|
||||
# OLD METHODS - DEPRECATE IN V0.5
|
||||
|
||||
@@ -7,6 +7,7 @@ module Impressionist
|
||||
def copy_config_file
|
||||
template 'impression.rb', 'config/initializers/impression.rb'
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,5 @@
|
||||
# Use this hook to configure impressionist parameters
|
||||
Impressionist.setup do |config|
|
||||
# Define ORM. Could be :active_record (default) and :mongo_mapper
|
||||
# Define ORM. Could be :active_record (default), :mongo_mapper or :mongoid
|
||||
# config.orm = :active_record
|
||||
end
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
ImpressionistController::InstanceMethods.send(:define_method, :direct_create_statement) do |query_params={}|
|
||||
# creates a statment hash that contains default values for creating an impression.
|
||||
# if :impressionable_id is a valid ObjectId then convert it into one
|
||||
base = (defined? Moped) ? Moped::BSON : BSON
|
||||
query_params.reverse_merge!(
|
||||
:impressionable_type => controller_name.singularize.camelize,
|
||||
:impressionable_id=> !base::ObjectId.legal?(params[:id]) ? params[:id] : base::ObjectId.from_string(params[:id])
|
||||
)
|
||||
associative_create_statement(query_params)
|
||||
end
|
||||
+7
-4
@@ -5,7 +5,7 @@ module Impressionist
|
||||
class Engine < Rails::Engine
|
||||
initializer 'impressionist.model' do |app|
|
||||
require "#{root}/app/models/impressionist/impressionable.rb"
|
||||
if Impressionist.orm == :active_record
|
||||
if Impressionist.orm == :active_record && defined? ActiveRecord
|
||||
require "impressionist/models/active_record/impression.rb"
|
||||
require "impressionist/models/active_record/impressionist/impressionable.rb"
|
||||
ActiveRecord::Base.send(:include, Impressionist::Impressionable)
|
||||
@@ -14,13 +14,16 @@ module Impressionist
|
||||
require "impressionist/models/mongo_mapper/impressionist/impressionable.rb"
|
||||
MongoMapper::Document.plugin Impressionist::Impressionable
|
||||
elsif Impressionist.orm == :mongoid
|
||||
require "impressionist/models/mongoid/impression.rb"
|
||||
require "impressionist/models/mongoid/impressionist/impressionable.rb"
|
||||
# Mongoid::Document Impressionist::Impressionable
|
||||
require 'impressionist/models/mongoid/impression.rb'
|
||||
require 'impressionist/models/mongoid/impressionist/impressionable.rb'
|
||||
Mongoid::Document.send(:include, Impressionist::Impressionable)
|
||||
end
|
||||
end
|
||||
|
||||
initializer 'impressionist.controller' do
|
||||
if Impressionist.orm == :mongoid
|
||||
require 'impressionist/controllers/mongoid/impressionist_controller.rb'
|
||||
end
|
||||
ActiveSupport.on_load(:action_controller) do
|
||||
include ImpressionistController::InstanceMethods
|
||||
extend ImpressionistController::ClassMethods
|
||||
|
||||
@@ -2,8 +2,12 @@ class Impression
|
||||
include Mongoid::Document
|
||||
include Mongoid::Timestamps
|
||||
|
||||
field :impressionable_type
|
||||
field :impressionable_id
|
||||
attr_accessible :impressionable_type, :impressionable_field, :impressionable_id, :user_id,
|
||||
:controller_name, :action_name, :view_name, :request_hash, :ip_address,
|
||||
:session_hash, :message, :referrer
|
||||
|
||||
belongs_to :impressionable, polymorphic: true
|
||||
|
||||
field :user_id
|
||||
field :controller_name
|
||||
field :action_name
|
||||
@@ -14,26 +18,15 @@ class Impression
|
||||
field :message
|
||||
field :referrer
|
||||
|
||||
belongs_to :impressionable, :polymorphic => true
|
||||
|
||||
after_save :update_impressions_counter_cache
|
||||
|
||||
def self.impressionist_count(options={})
|
||||
options.reverse_merge!(:filter => :request_hash, :start_date => nil, :end_date => Time.now)
|
||||
imps = options[:start_date].blank? ? impressions : impressions.where(:created_at.gte => options[:start_date], :created_at.lte => options[:end_date])
|
||||
options[:filter] == :all ? imps.count : imps.distinct(options[:filter]).count
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def update_impressions_counter_cache
|
||||
if self.referrer
|
||||
impressionable_class = self.impressionable_type.constantize
|
||||
set_callback(:create, :after) do |doc|
|
||||
unless impressionable_id.nil?
|
||||
impressionable_class = doc.impressionable_type.constantize
|
||||
|
||||
if impressionable_class.impressionist_counter_cache_options
|
||||
resouce = impressionable_class.find(self.impressionable_id)
|
||||
resouce.try(:update_impressionist_counter_cache)
|
||||
resource = impressionable_class.find(doc.impressionable_id)
|
||||
resource.try(:update_impressionist_counter_cache)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
+43
-3
@@ -4,9 +4,49 @@ module Impressionist
|
||||
|
||||
module ClassMethods
|
||||
def is_impressionable(options={})
|
||||
has_many :impressions, :as => :impressionable, :dependent => :destroy
|
||||
@impressionist_cache_options = options[:counter_cache]
|
||||
has_many :impressions, as: :impressionable, dependent: :destroy
|
||||
@impressionist_cache_options = options[:counter_cache]
|
||||
if !@impressionist_cache_options.nil?
|
||||
opts = impressionist_counter_cache_options
|
||||
field opts[:column_name], type: Integer
|
||||
end
|
||||
end
|
||||
|
||||
def impressionist_counter_cache_options
|
||||
if @impressionist_cache_options
|
||||
options = { :column_name => :impressions_count, :unique => false }
|
||||
options.merge!(@impressionist_cache_options) if @impressionist_cache_options.is_a?(Hash)
|
||||
options
|
||||
end
|
||||
end
|
||||
|
||||
def impressionist_counter_caching?
|
||||
impressionist_counter_cache_options.present?
|
||||
end
|
||||
|
||||
def counter_caching?
|
||||
::ActiveSupport::Deprecation.warn("#counter_caching? is deprecated; please use #impressionist_counter_caching? instead")
|
||||
impressionist_counter_caching?
|
||||
end
|
||||
end
|
||||
|
||||
def impressionable?
|
||||
true
|
||||
end
|
||||
|
||||
def impressionist_count(options={})
|
||||
options.reverse_merge!(:filter=>:request_hash, :start_date=>nil, :end_date=>Time.now)
|
||||
imps = options[:start_date].blank? ? impressions : impressions.between(created_at: options[:start_date]..options[:end_date])
|
||||
options[:filter] == :all ? imps.count : imps.where(options[:filter].ne => nil).count
|
||||
end
|
||||
|
||||
def update_impressionist_counter_cache
|
||||
cache_options = self.class.impressionist_counter_cache_options
|
||||
column_name = cache_options[:column_name].to_sym
|
||||
count = cache_options[:unique] ? impressionist_count(:filter => :ip_address) : impressionist_count
|
||||
old_count = send(column_name) || 0
|
||||
self.inc(column_name, (count - old_count))
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -38,6 +38,13 @@ describe Impression do
|
||||
@widget.reload
|
||||
}.should change(@widget, :impressions_count).from(0).to(1)
|
||||
end
|
||||
|
||||
it "should not update the timestamp on the impressable" do
|
||||
lambda {
|
||||
@widget.impressions.create(:request_hash => 'abcd1234')
|
||||
@widget.reload
|
||||
}.should_not change(@widget, :updated_at)
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
-5
@@ -1,5 +0,0 @@
|
||||
README.rdoc
|
||||
lib/**/*.rb
|
||||
bin/*
|
||||
features/**/*.feature
|
||||
LICENSE
|
||||
-20
@@ -1,20 +0,0 @@
|
||||
Copyright (c) 2009 jugyo
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining
|
||||
a copy of this software and associated documentation files (the
|
||||
"Software"), to deal in the Software without restriction, including
|
||||
without limitation the rights to use, copy, modify, merge, publish,
|
||||
distribute, sublicense, and/or sell copies of the Software, and to
|
||||
permit persons to whom the Software is furnished to do so, subject to
|
||||
the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
||||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
||||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
-53
@@ -1,53 +0,0 @@
|
||||
sunspot_mongoid
|
||||
====
|
||||
|
||||
A Sunspot wrapper for Mongoid.
|
||||
|
||||
Install
|
||||
----
|
||||
|
||||
gem install sunspot_mongoid
|
||||
|
||||
Examples
|
||||
----
|
||||
|
||||
class Post
|
||||
include Mongoid::Document
|
||||
field :title
|
||||
|
||||
include Sunspot::Mongoid
|
||||
searchable do
|
||||
text :title
|
||||
end
|
||||
end
|
||||
|
||||
For Rails3
|
||||
----
|
||||
|
||||
### as gem:
|
||||
|
||||
add a gem to Gemfile as following,
|
||||
|
||||
gem 'sunspot_mongoid'
|
||||
|
||||
### as plugin:
|
||||
|
||||
add gems to Gemfile as following,
|
||||
|
||||
gem 'sunspot'
|
||||
gem 'sunspot_rails'
|
||||
|
||||
and install sunspot_mongoid as rails plugin,
|
||||
|
||||
rails plugin install git://github.com/jugyo/sunspot_mongoid.git
|
||||
|
||||
Links
|
||||
----
|
||||
|
||||
* [sunspot](http://github.com/outoftime/sunspot)
|
||||
* [sunspot_rails](http://github.com/outoftime/sunspot/tree/master/sunspot_rails/)
|
||||
|
||||
Copyright
|
||||
----
|
||||
|
||||
Copyright (c) 2010 jugyo. See LICENSE for details.
|
||||
-57
@@ -1,57 +0,0 @@
|
||||
require 'rubygems'
|
||||
require 'rake'
|
||||
|
||||
begin
|
||||
require 'jeweler'
|
||||
Jeweler::Tasks.new do |gem|
|
||||
gem.name = "sunspot_mongoid"
|
||||
gem.summary = %Q{A Sunspot wrapper for Mongoid.}
|
||||
gem.description = %Q{A Sunspot wrapper for Mongoid that is like sunspot_rails.}
|
||||
gem.email = "jugyo.org@gmail.com"
|
||||
gem.homepage = "http://github.com/jugyo/sunspot_mongoid"
|
||||
gem.authors = ["jugyo"]
|
||||
gem.add_runtime_dependency "mongoid", ">= 0"
|
||||
gem.add_runtime_dependency "sunspot", ">= 1.1.0"
|
||||
gem.add_runtime_dependency "sunspot_rails", ">= 1.1.0"
|
||||
gem.add_development_dependency "shoulda", ">= 0"
|
||||
gem.add_development_dependency "rr", ">= 0"
|
||||
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
||||
end
|
||||
Jeweler::GemcutterTasks.new
|
||||
rescue LoadError
|
||||
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
||||
end
|
||||
|
||||
require 'rake/testtask'
|
||||
Rake::TestTask.new(:test) do |test|
|
||||
test.libs << 'lib' << 'test'
|
||||
test.pattern = 'test/**/test_*.rb'
|
||||
test.verbose = true
|
||||
end
|
||||
|
||||
begin
|
||||
require 'rcov/rcovtask'
|
||||
Rcov::RcovTask.new do |test|
|
||||
test.libs << 'test'
|
||||
test.pattern = 'test/**/test_*.rb'
|
||||
test.verbose = true
|
||||
end
|
||||
rescue LoadError
|
||||
task :rcov do
|
||||
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
||||
end
|
||||
end
|
||||
|
||||
task :test => :check_dependencies
|
||||
|
||||
task :default => :test
|
||||
|
||||
require 'rake/rdoctask'
|
||||
Rake::RDocTask.new do |rdoc|
|
||||
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
||||
|
||||
rdoc.rdoc_dir = 'rdoc'
|
||||
rdoc.title = "sunspot_mongoid #{version}"
|
||||
rdoc.rdoc_files.include('README*')
|
||||
rdoc.rdoc_files.include('lib/**/*.rb')
|
||||
end
|
||||
-1
@@ -1 +0,0 @@
|
||||
0.4.1
|
||||
@@ -1,36 +0,0 @@
|
||||
$:.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||
require 'sunspot_mongoid'
|
||||
|
||||
Mongoid.configure do |config|
|
||||
config.master = Mongo::Connection.new.db('sunspot-mongoid-test')
|
||||
end
|
||||
|
||||
# model
|
||||
class Post
|
||||
include Mongoid::Document
|
||||
field :title
|
||||
|
||||
include Sunspot::Mongoid
|
||||
searchable do
|
||||
text :title
|
||||
end
|
||||
end
|
||||
|
||||
# remove old indexes
|
||||
Post.destroy_all
|
||||
|
||||
# indexing
|
||||
Post.create(:title => 'foo')
|
||||
Post.create(:title => 'foo bar')
|
||||
Post.create(:title => 'bar baz')
|
||||
|
||||
# commit
|
||||
Sunspot.commit
|
||||
|
||||
# search
|
||||
search = Post.search do
|
||||
keywords 'foo'
|
||||
end
|
||||
search.each_hit_with_result do |hit, post|
|
||||
p post
|
||||
end
|
||||
-1
@@ -1 +0,0 @@
|
||||
require 'sunspot_mongoid'
|
||||
@@ -1,49 +0,0 @@
|
||||
require 'sunspot'
|
||||
require 'mongoid'
|
||||
require 'sunspot/rails'
|
||||
|
||||
# == Examples:
|
||||
#
|
||||
# class Post
|
||||
# include Mongoid::Document
|
||||
# field :title
|
||||
#
|
||||
# include Sunspot::Mongoid
|
||||
# searchable do
|
||||
# text :title
|
||||
# end
|
||||
# end
|
||||
#
|
||||
module Sunspot
|
||||
module Mongoid
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
extend Sunspot::Rails::Searchable::ActsAsMethods
|
||||
Sunspot::Adapters::DataAccessor.register(DataAccessor, base)
|
||||
Sunspot::Adapters::InstanceAdapter.register(InstanceAdapter, base)
|
||||
end
|
||||
end
|
||||
|
||||
class InstanceAdapter < Sunspot::Adapters::InstanceAdapter
|
||||
def id
|
||||
@instance.id
|
||||
end
|
||||
end
|
||||
|
||||
class DataAccessor < Sunspot::Adapters::DataAccessor
|
||||
def load(id)
|
||||
criteria(id).first
|
||||
end
|
||||
|
||||
def load_all(ids)
|
||||
criteria(ids)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def criteria(id)
|
||||
@clazz.criteria.id(id)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1 +0,0 @@
|
||||
require 'sunspot/mongoid'
|
||||
@@ -1,67 +0,0 @@
|
||||
# Generated by jeweler
|
||||
# DO NOT EDIT THIS FILE DIRECTLY
|
||||
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
||||
# -*- encoding: utf-8 -*-
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{sunspot_mongoid}
|
||||
s.version = "0.4.1"
|
||||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["jugyo"]
|
||||
s.date = %q{2011-03-04}
|
||||
s.description = %q{A Sunspot wrapper for Mongoid that is like sunspot_rails.}
|
||||
s.email = %q{jugyo.org@gmail.com}
|
||||
s.extra_rdoc_files = [
|
||||
"LICENSE",
|
||||
"README.md"
|
||||
]
|
||||
s.files = [
|
||||
".document",
|
||||
"LICENSE",
|
||||
"README.md",
|
||||
"Rakefile",
|
||||
"VERSION",
|
||||
"examples/example.rb",
|
||||
"init.rb",
|
||||
"lib/sunspot/mongoid.rb",
|
||||
"lib/sunspot_mongoid.rb",
|
||||
"sunspot_mongoid.gemspec",
|
||||
"test/helper.rb",
|
||||
"test/test_sunspot_mongoid.rb"
|
||||
]
|
||||
s.homepage = %q{http://github.com/jugyo/sunspot_mongoid}
|
||||
s.require_paths = ["lib"]
|
||||
s.rubygems_version = %q{1.6.0}
|
||||
s.summary = %q{A Sunspot wrapper for Mongoid.}
|
||||
s.test_files = [
|
||||
"examples/example.rb",
|
||||
"test/helper.rb",
|
||||
"test/test_sunspot_mongoid.rb"
|
||||
]
|
||||
|
||||
if s.respond_to? :specification_version then
|
||||
s.specification_version = 3
|
||||
|
||||
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
||||
s.add_runtime_dependency(%q<mongoid>, [">= 0"])
|
||||
s.add_runtime_dependency(%q<sunspot>, [">= 1.1.0"])
|
||||
s.add_runtime_dependency(%q<sunspot_rails>, [">= 1.1.0"])
|
||||
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
||||
s.add_development_dependency(%q<rr>, [">= 0"])
|
||||
else
|
||||
s.add_dependency(%q<mongoid>, [">= 0"])
|
||||
s.add_dependency(%q<sunspot>, [">= 1.1.0"])
|
||||
s.add_dependency(%q<sunspot_rails>, [">= 1.1.0"])
|
||||
s.add_dependency(%q<shoulda>, [">= 0"])
|
||||
s.add_dependency(%q<rr>, [">= 0"])
|
||||
end
|
||||
else
|
||||
s.add_dependency(%q<mongoid>, [">= 0"])
|
||||
s.add_dependency(%q<sunspot>, [">= 1.1.0"])
|
||||
s.add_dependency(%q<sunspot_rails>, [">= 1.1.0"])
|
||||
s.add_dependency(%q<shoulda>, [">= 0"])
|
||||
s.add_dependency(%q<rr>, [">= 0"])
|
||||
end
|
||||
end
|
||||
|
||||
-12
@@ -1,12 +0,0 @@
|
||||
require 'rubygems'
|
||||
require 'test/unit'
|
||||
require 'shoulda'
|
||||
require 'rr'
|
||||
|
||||
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
||||
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
||||
require 'sunspot_mongoid'
|
||||
|
||||
class Test::Unit::TestCase
|
||||
include RR::Adapters::TestUnit
|
||||
end
|
||||
@@ -1,51 +0,0 @@
|
||||
require 'helper'
|
||||
|
||||
#
|
||||
# NOTE: I think tests are too few...
|
||||
#
|
||||
class TestSunspotMongoid < Test::Unit::TestCase
|
||||
class Foo
|
||||
include Mongoid::Document
|
||||
field :title
|
||||
|
||||
include Sunspot::Mongoid
|
||||
searchable do
|
||||
text :title
|
||||
end
|
||||
end
|
||||
|
||||
class Bar
|
||||
include Mongoid::Document
|
||||
field :title
|
||||
|
||||
include Sunspot::Mongoid
|
||||
searchable(:auto_index => false, :auto_remove => false) do
|
||||
text :title
|
||||
end
|
||||
end
|
||||
|
||||
context 'default' do
|
||||
should 'sunspot_options is specified' do
|
||||
assert Foo.sunspot_options == {:include => []}
|
||||
assert Bar.sunspot_options == {:auto_index=>false, :auto_remove=>false, :include=>[]}
|
||||
end
|
||||
|
||||
should 'be called Sunspot.setup when call Foo.searchable' do
|
||||
mock(Sunspot).setup(Foo)
|
||||
Foo.searchable
|
||||
end
|
||||
|
||||
should 'get as text_fields from Sunspot::Setup' do
|
||||
text_field = Sunspot::Setup.for(Foo).all_text_fields.first
|
||||
assert text_field.type == Sunspot::Type::TextType.instance
|
||||
assert text_field.name == :title
|
||||
end
|
||||
|
||||
should 'search' do
|
||||
options = {}
|
||||
mock.proxy(Foo).solr_execute_search(options)
|
||||
mock(Sunspot).new_search(Foo) { mock(Object.new).execute }
|
||||
Foo.search(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user