dry

How to customize to_json response in Rails 3

倖福魔咒の 提交于 2020-01-10 12:54:31
问题 I am using respond_with and everything is hooked up right to get data correctly. I want to customize the returned json , xml and foobar formats in a DRY way, but I cannot figure out how to do so using the limited :only and :include . These are great when the data is simple, but with complex finds, they fall short of what I want. Lets say I have a post which has_many images def show @post = Post.find params[:id] respond_with(@post) end I want to include the images with the response so I could

Can I make this CSS simpler to avoid repeating the parent selector?

半腔热情 提交于 2020-01-10 06:04:56
问题 A common pattern I come across is the following: form.request input { /* ... */ } form.request input[type="text"] { /* ... */ } form.request select { /* ... */ } form.request br { /* ... */ } I have several lines beginning with the same selector ( form.request ), and I want to select various children. Can I do this in a neater way without the repetition (and preferably without additional dependencies like LESS)? Related question - if all the above comments contain the same styles, can I do

Can I make this CSS simpler to avoid repeating the parent selector?

百般思念 提交于 2020-01-10 06:03:10
问题 A common pattern I come across is the following: form.request input { /* ... */ } form.request input[type="text"] { /* ... */ } form.request select { /* ... */ } form.request br { /* ... */ } I have several lines beginning with the same selector ( form.request ), and I want to select various children. Can I do this in a neater way without the repetition (and preferably without additional dependencies like LESS)? Related question - if all the above comments contain the same styles, can I do

Replacement for column_names when using Mongoid with rails 3 and dry_crud

倾然丶 夕夏残阳落幕 提交于 2020-01-05 07:52:07
问题 I've been doing a spike on Rails 3 and Mongoid and with the pleasant memories of the auto scaffolding in Grails I started to look for a DRY view for ruby when I found: http://github.com/codez/dry_crud I created a simple class class Capture include Mongoid::Document field :species, :type => String field :captured_by, :type => String field :weight, :type => Integer field :length, :type => Integer def label "#{name} #{title}" end def self.column_names ['species', 'captured_by', 'weight', 'length

Classlist toggle on multiple elements

那年仲夏 提交于 2020-01-05 06:43:20
问题 I have functioning code, but I am sure there is a way to write it cleaner. My code is far from best practice I assume. Don't repeat yourself principle. I have tried looking for this problem but can not find an answer. Here are the expected result and my current code: https://jsfiddle.net/9ednsp6x/ document.getElementById("BtnMoreTotalt").onclick = function() {MoreBtnTotalt()}; function MoreBtnTotalt() { document.querySelector(".more-wrapper-totalt").classList.toggle("show"); } I also wonder,

Classlist toggle on multiple elements

耗尽温柔 提交于 2020-01-05 06:43:12
问题 I have functioning code, but I am sure there is a way to write it cleaner. My code is far from best practice I assume. Don't repeat yourself principle. I have tried looking for this problem but can not find an answer. Here are the expected result and my current code: https://jsfiddle.net/9ednsp6x/ document.getElementById("BtnMoreTotalt").onclick = function() {MoreBtnTotalt()}; function MoreBtnTotalt() { document.querySelector(".more-wrapper-totalt").classList.toggle("show"); } I also wonder,

DRY if statements

偶尔善良 提交于 2020-01-04 04:32:10
问题 I have a C++ program where in many different .cpp files, I do the something like this: if (!thing1.empty() && !thing2.empty()) { if (thing1.property < thing2.property) return func1(); else if (thing2.property < thing1.property) return func2(); else return func3(); } else if (!thing1.empty()) { return func1(); } else if (!thing2.empty()) { return func2(); } else { return func4(); } I'm trying to do func one way if thing1 is bigger than thing2, or backwards if the opposite is the case, but if

DRY when validating calculations made client side (Javascript) on the server (PHP)

十年热恋 提交于 2020-01-03 17:24:11
问题 I'm looking to DRY when I am validating a calculation made by the client (javascript) back on the server (PHP). I'm validating on the server to prevent a malicious user from duping the javascript, but I am calculating on the client to avoid the delay and server strain in AJAXing back to the server for the validation. My question: is there any way to do this DRY, or do I have to write the code out in both languages? If it has to be written out, is it better to AJAX back to the server for DRY

How do I make this algorithm lazier without repeating myself?

淺唱寂寞╮ 提交于 2020-01-03 10:05:39
问题 (Inspired by my answer to this question.) Consider this code (it's supposed to find the largest element that's less than or equal to a given input): data TreeMap v = Leaf | Node Integer v (TreeMap v) (TreeMap v) deriving (Show, Read, Eq, Ord) closestLess :: Integer -> TreeMap v -> Maybe (Integer, v) closestLess i = precise Nothing where precise :: Maybe (Integer, v) -> TreeMap v -> Maybe (Integer, v) precise closestSoFar Leaf = closestSoFar precise closestSoFar (Node k v l r) = case i

try… except… except… : how to avoid repeating code

守給你的承諾、 提交于 2020-01-02 07:24:10
问题 I'd like to avoid writting errorCount += 1 in more than one place. I'm looking for a better way than success = False try: ... else: success = True finally: if success: storage.store.commit() else: storage.store.rollback() I'm trying to avoid store.rollback() in every except clause. Any idea on how to do this? count = 0 successCount = 0 errorCount = 0 for row in rows: success = False count += 1 newOrder = storage.RepeatedOrder() storage.store.add(newOrder) try: try: newOrder.customer =