Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

temp

  • Loading branch information...
commit b0705c27f90ba89e88e12b6bed8d7dc667c072c0 2 parents 67e450e + 82798d0
@smalloranges smalloranges authored
View
1  .gitignore
@@ -20,3 +20,4 @@
#public/uploads/.DS_Store
.DS_Store
/public/system
+.rvmrc
View
58 .rvmrc
@@ -15,38 +15,66 @@ environment_id="ruby-1.9.3-p194@huacaor"
# return 1
# }
+#
+# Uncomment following line if you want options to be set only for given project.
+#
+# PROJECT_JRUBY_OPTS=( --1.9 )
+#
+# The variable PROJECT_JRUBY_OPTS requires the following to be run in shell:
+#
+# chmod +x ${rvm_path}/hooks/after_use_jruby_opts
+#
+
+#
# First we attempt to load the desired environment directly from the environment
# file. This is very fast and efficient compared to running through the entire
# CLI and selector. If you want feedback on which environment was used then
# insert the word 'use' after --create as this triggers verbose mode.
-if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
+#
+if [[ -d "${rvm_path:-$HOME/.rvm}/environments" \
&& -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
then
\. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
- [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
- \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
- if [[ $- == *i* ]] # check for interactive shells
- then echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
- else echo "Using: $GEM_HOME" # don't use colors in non-interactive shells
+
+ if [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]]
+ then
+ . "${rvm_path:-$HOME/.rvm}/hooks/after_use"
fi
else
# If the environment file has not yet been created, use the RVM CLI to select.
- rvm --create use "$environment_id" || {
+ if ! rvm --create use "$environment_id"
+ then
echo "Failed to create RVM environment '${environment_id}'."
return 1
- }
+ fi
fi
+#
+# If you use an RVM gemset file to install a list of gems (*.gems), you can have
+# it be automatically loaded. Uncomment the following and adjust the filename if
+# necessary.
+#
+# filename=".gems"
+# if [[ -s "$filename" ]]
+# then
+# rvm gemset import "$filename" | grep -v already | grep -v listed | grep -v complete | sed '/^$/d'
+# fi
+
# If you use bundler, this might be useful to you:
-# if [[ -s Gemfile ]] && {
-# ! builtin command -v bundle >/dev/null ||
-# builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
-# }
+# if [[ -s Gemfile ]] && ! command -v bundle >/dev/null
# then
-# printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
+# printf "The rubygem 'bundler' is not installed. Installing it now.\n"
# gem install bundler
# fi
-# if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
+# if [[ -s Gemfile ]] && command -v bundle
# then
-# bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
+# bundle install
# fi
+
+if [[ $- == *i* ]] # check for interactive shells
+then
+ echo "Using: $(tput setaf 2)$GEM_HOME$(tput sgr0)" # show the user the ruby and gemset they are using in green
+else
+ echo "Using: $GEM_HOME" # don't use colors in interactive shells
+fi
+
View
14 app/controllers/pictures_controller.rb
@@ -11,18 +11,11 @@ def create
flash[:notice] = flash_error("请选择图片")
redirect_to new_picture_path and return
end
-
- picture = Picture.new(
- :image => params[:filedata],
- :user_id => current_user.id,
- :desc => params[:desc].strip,
- :usage => Picture::FOR_PLANTS
- )
- picture.save
+ params[:user_id] = current_user.id
+ picture = Picture.create_picture(params)
unless params[:plant_zh_name].blank?
- plant = Plant.find_or_create_by_user_id_and_name(params[:plant_zh_name],current_user.id,params[:desc])
- plant.pictures << picture
+ plant = picture.create_parent_plant(params[:plant_zh_name].strip)
redirect_to edit_plant_path(plant,:picture_id => picture.id) and return
end
@@ -30,6 +23,7 @@ def create
end
def show
+ @current_user = current_user
@picture = Picture.includes(:plant).find(params[:id])
@plant = @picture.plant
@user = @picture.user
View
55 app/controllers/plants_controller.rb
@@ -20,46 +20,41 @@ def show
end
- def new
- @plant = Plant.new
- respond_to do |format|
- format.html
- format.json { render :json => @plant }
- end
- end
+ # def new
+ # @plant = Plant.new
+ # respond_to do |format|
+ # format.html
+ # format.json { render :json => @plant }
+ # end
+ # end
def edit
- #[["phylum_name","门",0],["sub_phylum_name","门",1],["pclass_name","纲",0],["sub_pclass_name","纲",0],["porder_name","目",0],['genus_name','属',0]]
- # @sub_phylum_name = Hash[PlantBaseInfo.sub_phylum_name]
- # @pclass_name = Hash[PlantBaseInfo.pclass_name]
- # @sub_pclass_name = Hash[PlantBaseInfo.sub_pclass_name]
- # @porder_name = Hash[PlantBaseInfo.porder_name]
- # @genus_name = Hash[PlantBaseInfo.genus_name]
@phylum_name = PlantBaseInfo.format_phylum_name
@plant = Plant.find(params[:id])
- @version_id = @plant.user_version_id(current_user.id)
+ @version = @plant.user_version(current_user.id)
+ @version_id = @version.nil? ? nil : @version.id
+ # phylum_id = @version.base_info_ids['phylum_id']
+ # pclass_id = @version.base_info_ids['pclass_id']
+ # porder_id = @version.base_info_ids['porder_id']
+ # genus_id = @version.base_info_ids['genus_id']
end
- def create
- plant = Plant.new
- params[:plant][:user_id] = current_user.id
- version_id = plant.update_by_params_data(params[:plant])
- plant.save
+ # def create
+ # params[:plant][:user_id] = current_user.id
+ # version_id = Plant.update_by_params_data(plant,params[:plant])
- unless params[:picture_id].blank?
- picture = Picture.find(params[:picture_id])
- plant.pictures << picture
- end
- return redirect_to picture_path(params[:picture_id]) unless params[:picture_id].blank?
- redirect_to plant_path(params[:id])
- # redirect_to edit_plant_path(plant, :version_id => version_id)
- end
+ # unless params[:picture_id].blank?
+ # picture = Picture.find(params[:picture_id])
+ # plant.pictures << picture
+ # end
+ # return redirect_to picture_path(params[:picture_id]) unless params[:picture_id].blank?
+ # redirect_to plant_path(params[:id])
+ # #redirect_to edit_plant_path(plant, :version_id => version_id)
+ # end
def update
- plant = Plant.find(params[:id])
params[:plant][:user_id] = current_user.id
- plant.update_by_params_data(params[:plant],params[:version_id])
- plant.save
+ Plant.update_by_params_data(params[:plant],params[:version_id],params[:id])
return redirect_to picture_path(params[:picture_id]) unless params[:picture_id].blank?
redirect_to plant_path(params[:id])
end
View
29 app/models/picture.rb
@@ -1,5 +1,8 @@
# encoding: utf-8
class Picture
+ FOR_PLANTS = 0
+ FOR_USER = 1
+ PLANT_METHODS = ['name','description','phylum_name','sub_phylum_name','pclass_name','sub_pclass_name','porder_name','genus_name']
include Mongoid::Document
include Mongoid::Timestamps
@@ -7,7 +10,7 @@ class Picture
FOR_USER = 1
field :usage, :type => Integer, :default => FOR_PLANTS
- # field :desc, :type => String
+ field :describe_info, :type => Hash, :default => {}
index :usage
@@ -18,23 +21,27 @@ class Picture
mount_uploader :image, ImageUploader
- def self.create_picture(filedata,usage=0)
+ def self.create_picture(data,usage=0)
picture = Picture.new
picture.usage = usage
- picture.image = filedata
+ picture.image = data[:filedata]
+ picture.describe_info = {:desc => data[:desc]}
+ picture.user_id = data[:user_id]
picture.save!
#FileUtils.rm_rf("#{Rails.root}/public/uploads")
return picture
end
- def plant_name
- plant = self.plant
- if plant.blank?
- return "待鉴定"
- elsif plant.zh_name.blank?
- return "未知"
- else
- return plant.zh_name
+ def create_parent_plant(name)
+ plant = Plant.find_or_create_by_user_id_and_name(name,user_id)
+ plant.pictures << self
+ plant
+ end
+
+ PLANT_METHODS.each do |method|
+ define_method "plant_#{method}" do |u_id=user_id|
+ plant.blank? ? "未知" : plant.send(method) #热门版本#plant.send(method,u_id) 个人版本
+ #instance_eval("self.plant.#{method} user_id")
end
end
View
76 app/models/plant.rb
@@ -1,27 +1,31 @@
# encoding: utf-8
-require File.join(Rails.root,'lib/shared_methods/instance_methods.rb')
+require File.join(Rails.root,'lib/shared_methods/shared_methods.rb')
class Plant
- include InstanceMethods
+ include SharedMethods
include Mongoid::Document
include Mongoid::Timestamps
+ BASE_INFO = PlantBaseInfo::HUAR_INFO.collect{|h_i| h_i[0..1]}
has_and_belongs_to_many :tags
- #belongs_to :user
has_many :pictures
embeds_many :versions
- field :zh_name
- index :zh_name
- BASE_INFO = PlantBaseInfo::HUAR_INFO.collect{|h_i| h_i[0..1]}
- def update_by_params_data(plant_data,version_id=nil)
+ field :name_list, :type => Array,:default => []
+ index :name_list
+
+ def self.update_by_params_data(plant_data,version_id=nil,plant_id=nil)
+
+ plant = plant_id.nil? ? Plant.new : Plant.find(plant_id)
+
unless version_id.blank?
- version = self.versions.find(version_id)
+ version = plant.versions.find(version_id)
version.update_by_params_data(plant_data)
else
- version = Version.new
+ version = plant.versions.new
version.update_by_params_data(plant_data)
- self.versions << version
end
version.save
- version.id
+ plant.name_list = plant.versions.collect{|v|v.name[:zh].nil? ? v.name['zh'] : v.name[:zh]}.flatten.uniq
+ plant.save
+ plant
end
def picture_path(thumb=nil)
@@ -32,40 +36,48 @@ def hot_version
self.versions.order_by(["points, DESC"]).first
end
+ ############################## 每个用户对应一个版本,没有就返回nil ##############################
def user_version(user_id)
versions.where(:user_id => user_id).first
end
- def user_version_id(user_id)
- user_version(user_id).nil? ? nil : user_version(user_id).id
- end
-
- def name
- hot_version && hot_version.name
+ def name(user_id=nil)
+ return nil if !user_id.nil? && user_version(user_id).nil?
+ user_id.nil? ? hot_version.name : user_version(user_id).name
end
- def description
- hot_version && hot_version.description
+ def description(user_id=nil)
+ return nil if !user_id.nil? && user_version(user_id).nil?
+ user_id.nil? ? hot_version.description : user_version(user_id).description
end
- def self.find_or_create_by_user_id_and_name(name,user_id,description=nil)
- plant = Plant.find_or_initialize_by(:zh_name => name)
- version = plant.user_version(user_id)
- plant.versions.create(:user_id => user_id,:description => description) if version.nil?
- plant.save
- plant
+ BASE_INFO.each do |b_i| # 获取植物的“门纲目科属”的名称
+ define_method b_i[0] do |user_id=nil| # 加user_id为指定版本的信息,不加为准确信息
+ return nil if !user_id.nil? && user_version(user_id).nil?
+ instance_eval("(user_id.nil? ? self.hot_version : user_version(user_id)).base_info_name.select{|n| n.match(/#{b_i[1]}$/)}")
+ end
end
+ ########################################## END ################################################
- BASE_INFO.each do |b_i|
- define_method b_i[0] do
- instance_eval("self.hot_version && self.hot_version.base_info_name.select{|n| n.match(/#{b_i[1]}$/)}")
+ def self.find_or_create_by_user_id_and_name(name,user_id)
+ plant = Plant.where(:name.in => [name]).first
+ plant = Plant.new if plant.nil?
+ version = plant.user_version(user_id)
+ if version.nil?
+ version = plant.versions.new
+ version.user_id = user_id
end
+ version.name[:zh] = name
+ plant.save
+ version.save
+ plant.update_attributes(:name_list => plant.versions.collect{|v|v.name['zh']}.flatten.uniq)
+ plant
end
end
-class Version
- include InstanceMethods
+class Version # 植物的不同版本
+ include SharedMethods
include Mongoid::Document
include Mongoid::Timestamps
embedded_in :plant
@@ -86,10 +98,6 @@ def update_by_params_data(plant_data)
self.user_id = plant_data[:user_id]
self.description = plant_data[:description]
self.base_info_ids = {:phylum_id => plant_data[:phylum_name]}
- # Version.fields.keys.each do |field|
- # # instance_eval("self.#{field} = #{plant_data[field.to_sym].to_s}") unless plant_data[field.to_sym].blank?
- # write_attribute(field.to_sym, plant_data[field.to_sym]) unless plant_data[field.to_sym].blank?
- # end
end
def base_info_name
View
19 app/models/plant_base_info.rb
@@ -1,13 +1,13 @@
# encoding: utf-8
-require File.join(Rails.root,'lib/shared_methods/instance_methods.rb')
+require File.join(Rails.root,'lib/shared_methods/shared_methods.rb')
class PlantBaseInfo
- include InstanceMethods
+ include SharedMethods
include Mongoid::Document
has_and_belongs_to_many :plants
- field :name, :type => Hash
+ field :name, :type => Hash #中、英、拉丁
field :usage, :type => Integer, :default => 0 #1(亚)
- field :parent_id, :type => Integer
- field :huar_home_id, :type => Integer
+ field :parent_id, :type => Integer # 标识上级的数字(自定义的数字)
+ field :huar_home_id, :type => Integer #当前所处层级的数字(自定义的数字)
field :description, :type => String
index "name.zh"
@@ -16,7 +16,7 @@ class PlantBaseInfo
HUAR_INFO = [["phylum_name","",0],["sub_phylum_name","",1],["pclass_name","",0],["sub_pclass_name","",0],["porder_name","",0],['genus_name','',0]]
- class << self
+ class << self # 获取植物的‘门纲目科属'的名字,加参数如english、lati分别获取英文和拉丁名。
HUAR_INFO.each do |h_i|
define_method "#{h_i[0]}" do |*args|
kinds = *args.first || 'zh'
@@ -25,16 +25,17 @@ class << self
end
end
- def self.format_phylum_name
+ def self.format_phylum_name # 将植物’门'的名字和id组合成hash供plants _form使用
Hash[PlantBaseInfo.where('name.zh' => /门$/, :usage => 0).only("name", "id").collect{|p|[p.name['zh'],p.id]}]
end
- def parent_name(kinds='zh')
+ # eg: 门、纲、目 纲的上级为门、下级为目
+ def parent_name(kinds='zh') #植物层级目录的上级名字
parent = PlantBaseInfo.where(:huar_home_id => parent_id).only("name").first
parent.blank? ? nil : parent.name[kinds]
end
- def children_name(kinds='zh')
+ def children_name(kinds='zh') # 植物层级目录下级名字
children = PlantBaseInfo.where(:parent_id => huar_home_id).only("name").first
children.blank? ? nil : children.name[kinds]
end
View
4 app/views/home/index.haml
@@ -7,6 +7,6 @@
%a{:href => picture_path(p.id)}
.p-top
.class
- .name= p.plant_name
+ .name= p.plant_name['zh']
- = image_tag(p.image.url(:medium), :alt => p.plant_name)
+ = image_tag(p.image.url(:medium), :alt => p.plant_name['zh'])
View
4 app/views/pictures/_picture_item.haml
@@ -2,5 +2,5 @@
%li
%a{:href => picture_path(p.id)}
.p-top
- .name= p.plant.zh_name
- %img{:alt => "", :src => p.image.url(:thumb), :style => "width:50px;height:50px;"}/
+ .name= p.plant_name['zh']
+ %img{:alt => "", :src => p.image.url(:thumb), :style => "width:50px;height:50px;"}/
View
2  app/views/pictures/new.haml
@@ -9,7 +9,7 @@
= file_field_tag "filedata"
%dt
- = label_tag :plant_zh_name, "中文名称:(选填)"
+ = label_tag :plant_name['zh'], "中文名称:(选填)"
%dd
= text_field_tag :plant_zh_name, ""
View
38 app/views/pictures/show.haml
@@ -1,23 +1,9 @@
.title
- - if @plant.zh_name.blank?
- %b 待鉴定
- - else
- %b= @plant.zh_name
-
- - if @plant.english_name
- = @plant.english_name
- - if @plant.latin_name.blank?
- - else
- |
- = @plant.latin_name
-
- - if @plant.blank?
- = link_to "[鉴定]", new_plant_path(:picture_id => @picture.id)
- - else
- = link_to "[鉴定]", edit_plant_path(@plant,:picture_id => @picture.id)
-
+ %b= @picture.plant_name['zh']
+ %b= @picture.plant_name['english']
+ %b= @picture.plant_name['latin']
.plant
- = link_to image_tag(@plant.picture_path, :alt => "@plant.zh_name"), "#"
+ = link_to image_tag(@picture.image.url(:page_size), :alt => @picture.plant_name['zh']), "#"
.slide-nav
.left-nav
.right-nav
@@ -31,12 +17,10 @@
.rt
%ul
%li
- %em>
- = link_to "木兰科", "#"
+ %em= @picture.plant_porder_name
latin
%li
- %em>
- = link_to "木兰属", "#"
+ %em= @picture.plant_genus_name
latin
%li
%em> 783
@@ -51,16 +35,12 @@
%p
= link_to @user.name, @user.link
2012-1-22 LX5 于上海
- &nbsp;
- = link_to "[编辑]", "javascript:void(0);" , :class => "edit-description"
.description
- = @picture.desc
+ = @picture.plant_name['zh']
+ = @picture.describe_info['desc']
%article
%p
- - if @plant.description.blank?
- 暂无介绍噢。
- - else
- = @plant.description
+ = @picture.plant_description
- if @plant.blank?
= link_to "[编辑]", new_plant_path(:picture_id => @picture.id)
- else
View
20 app/views/plants/_form.haml
@@ -24,16 +24,16 @@
%li
= f.select :phylum_name, @phylum_name.map{|p| p}, {:selected => @plant.phylum_name}
- / %li{ :style => @plant.sub_phylum_name ? '': 'display:none'}
- / = f.select :sub_phylum_name, @plant.sub_phylum_name
- / %li{:style => @plant.pclass_name == '[]' ? 'display:none' : ''}
- / = f.select :pclass_name, @plant.pclass_name
- / %li{:style => @plant.sub_pclass_name ? '': 'display:none'}
- / = f.select :sub_pclass_name,@plant.sub_pclass_name
- / %li{:style => @plant.porder_name ? '': 'display:none'}
- / = f.select :porder_name, @plant.porder_name
- / %li{:style => @plant.genus_name ? '': 'display:none'}
- / = f.select :genus_name, @plant.genus_name
+ %li{ :style => @plant.sub_phylum_name ? '': 'display:none'}
+ = f.select :sub_phylum_name, @plant.sub_phylum_name
+ %li{:style => @plant.pclass_name == '[]' ? 'display:none' : ''}
+ = f.select :pclass_name, @plant.pclass_name
+ %li{:style => @plant.sub_pclass_name ? '': 'display:none'}
+ = f.select :sub_pclass_name,@plant.sub_pclass_name
+ %li{:style => @plant.porder_name ? '': 'display:none'}
+ = f.select :porder_name, @plant.porder_name
+ %li{:style => @plant.genus_name ? '': 'display:none'}
+ = f.select :genus_name, @plant.genus_name
%dt
= f.label :characteristic, "特征:"
View
2  lib/shared_methods/instance_methods.rb → lib/shared_methods/shared_methods.rb
@@ -1,4 +1,4 @@
-module InstanceMethods
+module SharedMethods
NAME_KINDS = ['latin', 'english', 'zh']
NAME_KINDS.each do |n_k|
define_method "#{n_k}_name" do
Please sign in to comment.
Something went wrong with that request. Please try again.