Permalink
Browse files

temp

  • Loading branch information...
2 parents 67e450e + 82798d0 commit b0705c27f90ba89e88e12b6bed8d7dc667c072c0 @smalloranges smalloranges committed Jun 1, 2012
View
@@ -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
+
@@ -11,25 +11,19 @@ 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
redirect_to picture_path(picture)
end
def show
+ @current_user = current_user
@picture = Picture.includes(:plant).find(params[:id])
@plant = @picture.plant
@user = @picture.user
@@ -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
@@ -1,13 +1,16 @@
# 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
FOR_PLANTS = 0
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
@@ -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
Oops, something went wrong.

0 comments on commit b0705c2

Please sign in to comment.