How to remove whitespace from this code in haml? - ruby-on-rails-3

This is the code
.meta
= time_ago_in_words(discussion.created_at)
ago by
= discussion.user.username
|
= link_to "Edit", edit_discussion_path(discussion), class: "meta"
|
= link_to "Delete", discussion_path(discussion), method: :delete, confirm: "Are you sure?", class: "meta"
And this is the output:
<div class='meta'>
about 7 hours
ago by
foo
|
Edit
|
Delete
</div>
This makes the output have unbalanced space on either sides of the |.
Is it possible to remove the whitespace between these statements, without adding a span to everything, or just using erb or string interpolation?

Would explicitly printing it as a Ruby String help?
.meta
= time_ago_in_words(discussion.created_at)
ago by
= discussion.user.username
= "|"
= link_to "Edit", edit_discussion_path(discussion), class: "meta"
= "|"
= link_to "Delete", discussion_path(discussion), method: :delete, confirm: "Are you sure?", class: "meta"
I mean, it's not the nicest code and I'd prefer to just place the characters like your initial attempt, but since HAML forces indentation you probably have to take some sort of different path for specifying whitespace/space-characters. I figure doing this might give you the simple means of precision as far as adding a specific number of spaces or characters (if you need multiple) to either side.
EDIT: I noticed the title specifies "remove" whitespace, rather than "balance" it - my mistake. As such you can try succeed, as explained here: Haml: Control whitespace around text.
Succeed without a follow up, or followed by the empty String, should probably achieve what you want without any whitespace.
EDIT 2: You're absolutely correct, the > modifier doesn't work with ruby print lines (like link to ). Succeed should still work, or you could create a helper function that concatenates all of these lines into a single string.

As of right now (October 21, 2012), there's no way to elegantly do this.
If it is possible when you read this, please do answer the question, and I'll mark it as the answer.

Open ur code in adobe dreamwever and pres ctrl + f ..
write below code and with tick regular expression box
in find box : [\r\n]{2,}
in replace box : \n
n replace all...
enjoyyyy

Related

rebol parsing html: get error "title has no value"

I'm trying to parse an html page:
url: https://dzone.com/articles/2-entity-framework-alternatives-or-give-me-data
html: read url
parse html [
to {<h1 class="article-title" itemprop="headline">}
thru {<h1 class="article-title" itemprop="headli
ne">}copy title to {</h1>}
]
probe title
Can't see why it doesn't work since I get error "title has no value"
I assume that you're using Rebol/view since the free versions don't do https though rebol3 does.
If you want to see if something is working you should look at the return value of the parse, and you'll see it's false which means that there's a problem with your parse rule. Anyway, this works for me though the quotes around the tags are not necessary as < and > are both string delimiters.
>> parse html [
thru <h1 class="article-title" itemprop="headline">
thru <h1 class="article-title" itemprop="headline">
copy title to </h1> to end
]
== true
>> trim/head/tail title
== "2 Entity Framework Alternatives (or Give Me Data!)"
It does not work most probably because the first to stops before the matched string, so that thru starts at the beginning of the first occurence of <h1 ...>, not at the second as you might have expected. You need to skip the first occurrence, before trying to search for the second one. You can achieve that using two thru rules as shown in another answer, or just repeat the rule twice to avoid duplicating it:
parse html [
2 thru <h1 class="article-title" itemprop="headline">
copy title to </h1> to end
]
Notice the final to end rule, which will make parse return true if your rules succeed in reaching the end. The to end rule is a placeholder rule, as you do not care about what is following </h1>, but want to reach the end of the input anyway.
EDIT: Testing the code you submitted works fine from here unchanged. The editing of your question has actually fixed the cause of the error. I can reproduce your issue with your original code.

Is it possible to call truncate method in rspec feature test?

I'm using Rails 3.2, Rspec 2 and capybara 2.
I am truncating a string of body text in my webpage and would like to test this in my feature spec.
Can someone advise how to do this because I can't seem to call truncate in my test because it keeps saying it's an undefined method and I don't really want to have a long string of truncated text in my test suite; just applying the truncate function is good enough.
I've tried using helper.truncate(), view.truncate() but to no avail.
I've used the Faker gem in other parts of my tests if there's a way to generate a lorem ipsum string and truncate it to compare against somehow.
View code:
<dd><%= truncate(project.details, :length => 100) %></dd>`
Test code
it { should have_selector('dd', #project.details) }
This test test worked fine when I was showing the full details text but because I've decided to only show the first 100 characters in this view I'm not sure how to test for this without having to set the details as a fixed string and then check for a truncated version of it somehow.
Thanks
Col
truncate is a Rails extension to the String class and, as such, is not available in Ruby or RSpec. There may be way to "include" it in some fashion, but I'm familiar with how to do that. It turns out that the definition is self-contained, however, so defining it yourself is straightforward. Here is the source taken from http://apidock.com/rails/String/truncate. It's defined to an instance method, so if you want to use it with a string that's passed in, you'd need to include a text parameter in lieu of referencing self as is currently done in the first line of the body.
# File activesupport/lib/active_support/core_ext/string/filters.rb, line 38
def truncate(length, options = {})
text = self.dup
options[:omission] ||= "..."
length_with_room_for_omission = length - options[:omission].mb_chars.length
chars = text.mb_chars
stop = options[:separator] ?
(chars.rindex(options[:separator].mb_chars, length_with_room_for_omission) || length_with_room_for_omission) : length_with_room_for_omission
(chars.length > length ? chars[0...stop] + options[:omission] : text).to_s
end
This works for me :)
require 'active_support/core_ext/string/filters'
it { should have_selector('dd', #project.details.truncate(100)) }
In the end I decided that if the page was being viewed on a larger screen then the wider column would allow more of the details to be shown and reduce the need for truncating the text so I decided to trim the details using css instead to reduce what was shown onscreen using the following:
.text-trim {
overflow: hidden;
white-space: nowrap;
text-overflow: ellipsis;
}

Deleting content from text field with Capybara

I'm writing a script that fills out text fields with Capybara, but prior to filling out the fields, I want to ensure that the fields are empty and that text is not autofilled. Basically, I'm looking for the opposite of
(Object) fill_in(locator, options = {}) #empty_content_of? delete?
found here: http://rubydoc.info/github/jnicklas/capybara/master/Capybara/Node/Actions. Advice?
For me only this solution worked:
fill_in('Foo', with: 'bar', fill_options: { clear: :backspace })
I have Vue.js on frontend.
After struggling with this, I asked a coworker and the solution was to use the following:
fill_in(locator, with: "")
So, for example:
fill_in "Name", with: ""
This makes perfect sense and is probably intuitive to many, but I was stumped and couldn't find an answer on SO so I thought I would post about it in case it helps anyone.
you can use the native selenium bindings to clear an input field without filling in an empty string
element = find('locator')
element.native.clear
I prefer this option rather than fill_in.
Also if you think about it fill in is limited to find your locator by label or name so if it doesn't have a label or name you still have to use find
A solution that works for me and has always been reliable:
field = find('locator')
field.value.length.times { field.send_keys [:backspace] }
In Capybara terms of mimicking a user's behaviour, this also seems a correct way to me of doing that.
For React you've got to do more than that. fill_in field, with: '' does native.clear. Which doesn't play nicely with React. As doesn't fill_in field, with: 'some text'. Since it does arguments[0].value = '' before typing text.
I've run into the issues with react-datetime. What I've settled with is:
def fill_in_react_datetime n, options
with = options[:with] == '' ? '' : format_date(options[:with])
fill_in n, with: with, fill_options: {clear: [[:control, 'a'], :delete]}
end
Building on the accepted answers, you can optionally pass with: nil:
e.g.
fill_in("search-input", with: nil, fill_options: { clear: :backspace })
I prefernil over an empty string ("") simply for explicitness. I am explicitly not setting the value and clearing with backspace. Makes for an understandable test.

How could I escape a & in Haml so that it compiles to & instead of &? (Haml noob)

I am trying to use the Icomoon icon font with Haml and can't seem to find a way to escape the & so that it stays just an & instead of &.
The Icomoon font allows you to use HTML entities with a data-icon="" attribute. Works smooth as butter in HTML and even in a Haml file if I just do a straight HTML link.
However, since I'm learning Haml I thought I'd see if anyone on here would like to recommend the best way to approach this.
Here's a sample of what happens.
This is the original Haml:
%a(href='/posts' data-icon="&#x0026" aria-hidden='true')
This is how it compiles:
<a aria-hidden='true' data-icon='&#x0026' href='/posts'>
This is how it needs to compile for the icon font to work:
<a aria-hidden='true' data-icon='&#x0026' href='/posts'>
and here is a codepen where you can see how the icon renders due to the amp; addition: http://codepen.io/dandenney/pen/3/6
I didn't like the top poster's way of completing this question. So far the best way I've found is to do:
- foo = "&#x0026".html_safe
%a(href='/posts' data-icon=foo aria-hidden='true')
I'm not fully happy with this, but think it's better for rails apps rather than turning off HTML escaping everywhere.
You can use the :escape_attrs option to control whether HTML sensitive characters in attributes are escaped:
require 'haml'
haml = "%a(href='/posts' data-icon=\"&#x0026\" aria-hidden='true')"
puts Haml::Engine.new(haml, :escape_attrs => false).to_html
Output:
<a aria-hidden='true' data-icon='&#x0026' href='/posts'></a>
Note that this will apply to all attributes in your Haml template.
In my opinion, I don't like the idea to disable the feature to escape the characters generally. Maybe you use relay at some point in your application on it.
For me the best way to do it is:
%a{ href: '/', 'data-icon' => "✐".html_safe }

haml editing on gEdit, any way to auto indent?

I'm using gEdit to edit haml files and the auto indenting feature seems to work when i'm writing one line after another, but when I go and clear out a exisiting element (a div or a class) the rest of the code below doesn't adjust itself. Suppose this is my code
.rightside
.container
%ul
%li hello
%li world
Now is there any way (using some plugin) through which when I delete .rightside, the rest of the code moves to left by two spaces? I now have to manually erase the space on every line. Thanks for your input.
You can select the block underneath the tag in question and use Shift-Tab or Ctrl-Shift-T to un-indent the text, assuming you have gedit set to use 2 spaces for tabs.
I have also installed the multi-edit plugin. This gives me multiple carets at which points any edits i make will be duplicated. In this case, I can just position the cursor at the top of that block, use the Ctrl-PgDown key shortcut to select the whole block, and then hit delete a couple times.