美文网首页
(3)全局修改价格与number_with_precision

(3)全局修改价格与number_with_precision

作者: suhuanzhen | 来源:发表于2016-11-27 21:18 被阅读0次

把文件权限从版本控制中移除

参考资料: http://www.01happy.com/git-ignore-filemode/

项目文件的权限默认被纳入版本控制,所以我们应该在第一个笔记时就把文件权限从版本控制中移除,这样本地修改了项目某个文件的权限,在服务器和git仓库都不会发生变动。前面我们并没有修改文件权限,所以我们现在就把文件权限从版本控制中移除

.../active_record_first# git config core.filemode false
.../active_record_first# cat .git/config

[core]
    repositoryformatversion = 0
    filemode = false
    bare = false
    logallrefupdates = true
[branch "master"]
    remote = https://github.com/xiaohuacc/active_record.git
    merge = refs/heads/master
[branch "second/delegate"]
    remote = https://github.com/xiaohuacc/active_record.git
    merge = refs/heads/second/delegate

如上,通过cat命令查看到filemodel = false就行了,这样我们本地文件权限的修改都不会影响到git仓库和服务器

案例

我们切到新的分支进行开发

git checkout -b third/number

场景

有时我们项目中的一个字段需要修改显示,但是很多地方都引用了该字段,一个个修改很麻烦。

场景1:在项目所有视图文件中product.Price都需要修改掉,让所有商品的价格全部上调100
  • 我们被限制不能直接修改数据表,因为数据表的数据不希望被修改;
  • 我们很难找到在所有地方,包括接口、视图文件、控制器、模型中修改每个访问商品的Price字段的地方一个个修改

方法:
我们直接修改Price字段,只要修改一个地方,其他所有地方不需要进行变更。


image.png

添加路由


image.png

添加动作

#thirddef number_one  @products = Product.limit(3)enddef number_two  @products = Product.limit(3)enddef number_three  @products = Product.limit(3)end

添加视图文件


image.png

可以看到结果所有访问商品价格的地方没有做任何修改,价格就自动变化为增加1000了


image.png
场景2:

在项目所有视图文件中product.Price都需要修改掉,让所有商品的价格固定显示两位小数,四舍五入。空就不显示,为0则显示0.00。不过这时候我们就不能再使用上面那种方法了,要让0显示0.00那么就使用number_with_precision方法,但是该方法返回的结果是字符串,所以不能进行逻辑运算。

办法就是,我们在模型文件定义price_a方法,在不涉及逻辑运算的地方用product.price_a替换product.Price.
涉及逻辑运算的地方则不使用该方法,而是直接对结果使用number_with_precision方法

image.png

在模型文件中使用number_with_precision方法需要引入ActionView::Helpers。而在帮助文件或者视图文件,都不需要引入该模块。

image.png image.png
场景3:

在项目中含有很多表都含有价格字段,比如products和product_infoes和运算后的价格结果;而且只在某些地方需要修改显示。这时我们就不要在每个模型文件定义这么一个方法了,我们直接在帮助文件定义一个帮助方法,在需要调整显示格式的地方我们调用该帮助方法即可。

帮助方法不能在js、coffee文件和模型文件里面使用

image.png
def number_three  
  @products = Product.includes(:product_info).limit(3)
end
image.png image.png

提交到git仓库

git add .
git commit -m "全局修改价格与number_with_precision"
git push -u https://github.com/xiaohuacc/active_record.git third/number

git checkout master
git merge third/number

Updating 6dd2565..18e06bd
Fast-forward
 .idea/workspace.xml                      | 125 +++++++++++++++++++++++++++++++++++++++++++---------------
 app/controllers/products_controller.rb   |  11 ++++++
 app/helpers/application_helper.rb        |   6 +++
 app/models/product.rb                    |  14 +++++++
 app/views/products/number_one.html.erb   |   4 ++
 app/views/products/number_three.html.erb |   7 ++++
 app/views/products/number_two.html.erb   |  11 ++++++
 config/routes.rb                         |   6 +++
 8 files changed, 152 insertions(+), 32 deletions(-)
 create mode 100644 app/views/products/number_one.html.erb
 create mode 100644 app/views/products/number_three.html.erb
 create mode 100644 app/views/products/number_two.html.erb

相关文章

网友评论

      本文标题:(3)全局修改价格与number_with_precision

      本文链接:https://www.haomeiwen.com/subject/sjsnpttx.html