我如何在红宝石哈希值之间进行“漂亮打印”比较,以便用户可以在Rails应用程序中轻松查看它们之间的差异?

问题描述 投票:0回答:1

我在rails应用程序中具有版本历史记录功能,该功能通过将当时模型的as_json表示形式保存到数据库列来记录活动记录模型的快照。

模型数据是关系数据,具有几个嵌套的has_many关联,也需要向用户显示。

我现在想为用户提供一种比较版本的简便方法,类似于您在Wordpress或Github中可能找到的版本。 (example image

我发现this gem效果很好,但是它只接受字符串作为输入。

我不能保证在给定对象中将出现哪些顶级和嵌套键。

所以,我想我想做的是:

  1. 将存储的对象重构为Ruby哈希(已完成)
  2. 将对象的存储版本打印为YAML,使用换行符和选项卡使用户易于理解
  3. 将字符串输入到diffy gem中,并与不同版本的字符串进行比较。

或者,我可以将模型显示为HTML(简单),并以某种方式将HTML与HTML进行比较(看似困难,似乎没有差异的宝石可以做到这一点,而手动找出它似乎超出了我的能力)。

仅打印散列的to_yaml表示会产生难看的YAML输出,而没有制表符或换行符,当我尝试通过YAML验证程序运行它时,验证将失败。不知道为什么会这样。

我正在使用的数据类型的示例。注意嵌套的关联数据。

 {"id"=>6, "name"=>"Abbey View Primary Academy", "organisation_id"=>3, "description"=>"Abbey View Primary Academy will be committed to delivering inspirational learning opportunities that are fun, purposeful and challenging. We want our children not just to achieve, but to exceed expectations and attain the very best results they can academically, creatively and personally. Our pupils will take ownership of their learning, becoming highly aspirational and resilient learners who are able to make positive contributions to life in modern Britain.\r\nOur pupils will be at the heart of every decision we make and we will offer a safe, nurturing and creative learning environment, which will enable us to foster a passion for learning and curiosity in a happy and supported way.", "url"=>"http://www.abbeyviewprimaryacademy.org/home", "visible"=>true, "email"=>"[email protected]", "visible_from"=>nil, "visible_to"=>nil, "created_at"=>"2020-06-04T17:09:13.965Z", "updated_at"=>"2020-06-06T14:00:21.420Z", "discarded_at"=>nil, "approved"=>true, "notes_count"=>0, "ofsted_reference_number"=>nil, "old_ofsted_external_id"=>nil, "label_list"=>[], "taxonomies"=>[{"id"=>318, "name"=>"Education and learning", "created_at"=>"2020-06-04T17:50:16.033Z", "updated_at"=>"2020-06-04T17:50:16.033Z", "parent_id"=>1, "locked"=>nil, "sort_order"=>4}, {"id"=>319, "name"=>"Primary schools", "created_at"=>"2020-06-04T17:50:16.108Z", "updated_at"=>"2020-06-04T17:50:16.108Z", "parent_id"=>318, "locked"=>nil, "sort_order"=>0}, {"id"=>255, "name"=>"Communication and Interaction", "created_at"=>"2020-06-04T17:09:13.793Z", "updated_at"=>"2020-06-04T17:09:13.793Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>3}, {"id"=>254, "name"=>"Hearing", "created_at"=>"2020-06-04T17:09:13.766Z", "updated_at"=>"2020-06-04T17:09:13.766Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>2}, {"id"=>253, "name"=>"Social, Emotional and Mental Health Difficulties", "created_at"=>"2020-06-04T17:09:13.728Z", "updated_at"=>"2020-06-04T17:09:13.728Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>1}, {"id"=>252, "name"=>"Autism", "created_at"=>"2020-06-04T17:09:13.661Z", "updated_at"=>"2020-06-04T17:09:13.661Z", "parent_id"=>251, "locked"=>nil, "sort_order"=>0}], "locations"=>[{"id"=>6, "name"=>"Abbey View Primary Academy", "description"=>nil, "latitude"=>"51.616709", "longitude"=>"-0.745362", "address_1"=>" Kennedy Avenue", "city"=>"High Wycombe", "state_province"=>"Buckinghamshire", "postal_code"=>"HP11 1BX", "country"=>"GB", "google_place_id"=>nil}], "contacts"=>[{"service_id"=>6, "id"=>6, "name"=>"Abbey View Primary Academy", "title"=>"", "visible"=>false}], "cost_options"=>[], "regular_schedules"=>[]} 
ruby-on-rails ruby yaml diff
1个回答
0
投票

您可以使用JSON.pretty_generate

JSON.pretty_generate
© www.soinside.com 2019 - 2024. All rights reserved.