diff --git a/app/controllers/open_source_projects_controller.rb b/app/controllers/open_source_projects_controller.rb index a77ac858e..fa1f19058 100644 --- a/app/controllers/open_source_projects_controller.rb +++ b/app/controllers/open_source_projects_controller.rb @@ -18,6 +18,7 @@ class OpenSourceProjectsController < ApplicationController per_page_option = 10 @open_source_projects = OpenSourceProject.filter(@app_dir, @language, @created_at) + @open_source_projects = @open_source_projects.like(params[:name]) if params[:name].present? @os_project_count = @open_source_projects.count @os_project_pages = Paginator.new @os_project_count, per_page_option, params['page'] @@ -65,17 +66,6 @@ class OpenSourceProjectsController < ApplicationController order(sort_clause). all - - - # @offset, @limit = api_offset_and_limit({:limit => 10}) - # @forum = Forum.find(params[:id]) - # @memos_all = @forum.topics - # @topic_count = @memos_all.count - # @topic_pages = Paginator.new @topic_count, @limit, params['page'] - - # @offset ||= @topic_pages.offset - # @memos = @memos_all.offset(@offset).limit(@limit).all - respond_to do |format| format.html { render :layout => "base_opensource_p" @@ -148,6 +138,28 @@ class OpenSourceProjectsController < ApplicationController end end + def remove_condition + @app_dir = params[:app_dir] + @language = params[:language] + @created_at = params[:created_at] + redirect_to open_source_projects_path(:app_dir => @app_dir, :language => @language, :created_at => @created_at, :name => params[:name]) + end + + def search + per_page_option = 10 + + @open_source_projects = OpenSourceProject.filter(@app_dir, @language, @created_at) + @open_source_projects = @open_source_projects.like(params[:name]) if params[:name].present? + + @os_project_count = @open_source_projects.count + @os_project_pages = Paginator.new @os_project_count, per_page_option, params['page'] + + @open_source_projects = @open_source_projects.offset(@os_project_pages.offset).limit(@os_project_pages.per_page) + + redirect_to open_source_projects_path(:name => params[:name]) + + end + private def require_master diff --git a/app/helpers/open_source_projects_helper.rb b/app/helpers/open_source_projects_helper.rb index 1ae851569..6c9060c18 100644 --- a/app/helpers/open_source_projects_helper.rb +++ b/app/helpers/open_source_projects_helper.rb @@ -1,22 +1,9 @@ module OpenSourceProjectsHelper - def render_opensource_project(os_projects) - s=''.html_safe - s << "" - end - - def show_condition(app_dir, language, created_at) + def show_condition(app_dir, language, created_at, name) s=''.html_safe unless app_dir.nil? s_temp = content_tag('a', app_dir) - temp = link_to 'x', {:controller => "tags", :action => "remove_tag"}, :remote => true + temp = link_to 'x', {:controller => "open_source_projects", :action => "remove_condition", :language => language, :created_at => created_at, :name => name} temp = content_tag('span', temp, :class => 'del') s_temp << temp s_temp = content_tag('span', s_temp, :class => 'tag_show') @@ -25,7 +12,7 @@ module OpenSourceProjectsHelper end unless language.nil? s_temp = content_tag('a', language) - temp = link_to 'x', {:controller => "tags", :action => "remove_tag"}, :remote => true + temp = link_to 'x', {:controller => "open_source_projects", :action => "remove_condition", :app_dir => app_dir, :created_at => created_at, :name => name} temp = content_tag('span', temp, :class => 'del') s_temp << temp s_temp = content_tag('span', s_temp, :class => 'tag_show') @@ -33,7 +20,7 @@ module OpenSourceProjectsHelper end unless created_at.nil? s_temp = content_tag('a', created_at) - temp = link_to 'x', {:controller => "tags", :action => "remove_tag"}, :remote => true + temp = link_to 'x', {:controller => "open_source_projects", :action => "remove_condition", :app_dir => app_dir, :language => language, :name => name} temp = content_tag('span', temp, :class => 'del') s_temp << temp s_temp = content_tag('span', s_temp, :class => 'tag_show') @@ -42,5 +29,4 @@ module OpenSourceProjectsHelper s = content_tag('div', s, :id => 'tags') # s = content_tag('div', s, :class => 'tags') end - end diff --git a/app/models/open_source_project.rb b/app/models/open_source_project.rb index 95b4444e1..e526dc921 100644 --- a/app/models/open_source_project.rb +++ b/app/models/open_source_project.rb @@ -1,6 +1,6 @@ class OpenSourceProject < ActiveRecord::Base attr_accessible :name - + include Redmine::SafeAttributes has_many :applies, :class_name => "ApplyProjectMaster", :as => :apply, :dependent => :delete_all has_many :topics, :class_name => 'RelativeMemo', :foreign_key => 'osp_id', :conditions => "#{RelativeMemo.table_name}.parent_id IS NULL", :order => "#{RelativeMemo.table_name}.created_at DESC", :dependent => :destroy @@ -11,28 +11,36 @@ class OpenSourceProject < ActiveRecord::Base has_many :admin, :through => :masters, :class_name => 'User' has_many :apply_tips, :class_name => 'ApplyProjectMaster', :as => :apply, :dependent => :delete_all, :conditions => "#{ApplyProjectMaster.table_name}.status = 1" has_many :applicants, :class_name => 'User', :through => :apply_tips, :source => :user - + validates_uniqueness_of :name - - acts_as_taggable - + + acts_as_taggable + scope :applied_by, lambda { |user_id| { :include => :apply_project_master, - :conditions => ["#{ApplyProjectMaster.table_name}.user_id = ?", user_id] } - } - + :conditions => ["#{ApplyProjectMaster.table_name}.user_id = ?", user_id] + } + } + scope :like, lambda {|arg| + if arg.blank? + where(nil) + else + pattern = "%#{arg.to_s.strip.downcase}%" + where("LOWER(name) LIKE :p OR LOWER(description) LIKE :p ", :p => pattern) + end + } def filter(app_dir, language, created_at) filter_app_dir(app_dir).filter_language(language).filter_time(created_at) end - + def self.filter(app_dir, language, created_at) self.filter_app_dir(app_dir).filter_language(language).filter_time(created_at) end - + scope :filter_app_dir, lambda {|args| nil } - + scope :filter_language, lambda {|*arg| if arg[0].nil? where(nil) @@ -40,88 +48,87 @@ class OpenSourceProject < ActiveRecord::Base tagged_with(arg).order('updated_at desc') end } - + scope :filter_time, lambda {|args| where("YEAR(#{OpenSourceProject.table_name}.created_at) = ?", args) unless args.nil? } - + # def filter_app_dir(app_dir) - # nil + # nil # end -# + # # def self.filter_app_dir(app_dir) - # nil + # nil # end -# + # # def filter_language(language) - # nil + # nil # end -# + # # def self.filter_language(language) - # nil + # nil # end -# + # # def filter_time(created_at) - # nil + # nil # end -# + # # def self.filter_time(created_at) - # nil + # nil # end - - + def short_description(length = 255) description.gsub(/^(.{#{length}}[^\n\r]*).*$/m, '\1...').strip if description end - + def applied_by?(user) self.applies.each do |apply| if apply.user_id == user.id - return true - end + return true + end end false end - + def allowed?(user) self.applies.each do |apply| if apply.user_id == user.id and apply.status == 2 - return true - end + return true + end end false end - + def set_apply(user, flag=true) flag ? set_filter(user) : remove_filter(user) end - + def set_filter(user) self.applies << ApplyProjectMaster.new(:user => user, :status => 1) end - + def remove_filter(user) return nil unless user && user.is_a?(User) ApplyProjectMaster.delete_all "apply_type = '#{self.class}' AND apply_id = #{self.id} AND user_id = #{user.id}" end - + def admin?(user) if user.admin? or ApplyProjectMaster.find(:all, :conditions => ["user_id = ? and apply_type = 'OpenSourceProject' and apply_id = ? and status = ?", user.id, self.id, 2]).present? - return true - else - return false + return true + else + return false end end - + def reset_counters! self.class.reset_counters!(id) end - + def self.reset_counters!(id) osp_id = id.to_i update_all("topic_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NULL)," + " memo_count = (SELECT COUNT(*) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id} AND parent_id IS NOT NULL)," + " last_memo_id = (SELECT MAX(id) FROM #{RelativeMemo.table_name} WHERE osp_id=#{osp_id})", ["id = ?", osp_id]) - end + end end diff --git a/app/views/open_source_projects/index.html.erb b/app/views/open_source_projects/index.html.erb index 6aca9d2d4..0e9f1008a 100644 --- a/app/views/open_source_projects/index.html.erb +++ b/app/views/open_source_projects/index.html.erb @@ -8,7 +8,6 @@ @@ -144,9 +143,8 @@ li {
  • 查找条件>
  • - - <%= show_condition(@app_dir, @language, @created_at) %> + <%= show_condition(@app_dir, @language, @created_at, params[:name]) %>
  • <%= @os_project_count %> 个开源项目
  • @@ -160,24 +158,24 @@ li {