
Security News
Nx npm Packages Compromised in Supply Chain Attack Weaponizing AI CLI Tools
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
In an effort to encourage even MORE separation of concerns in your Controllers
and Layouts, the motion-kit-events
gem provides a way to listen for custom
events (usually triggered by buttons or other UI events) and respond in your
controller. This keeps your views from being cluttered with business logic and
your controllers from being cluttered with view code.
An example of a UIViewController using MotionKit::Events:
class LoginController < UIViewController
def viewDidLoad
@layout = LoginLayout.new(root: self.view).build
@layout.on :login { |username, password| initiate_login(username, password) }
@layout.on :forgot_password { show_forgot_password }
@layout.on :help { show_help }
end
def initiate_login(username, password)
@layout.pause_ui
# send login info to the API
API::Client.login(username, password) do |user, errors|
handle_login_response(user, errors)
end
end
def handle_login_response(user, errors)
# ...
@layout.resume_ui
end
# ...
end
Now we can test just the behavior of the controller. When it receives a
:login
event, it should send a login
request to its API and handle
user
or errors
.
class LoginLayout < MK::Layout
def layout
add UITextField, :username_field do
delegate self
end
add UITextField, :password_field do
delegate self
end
add UIButton, :login_button do
on :touch do # This is Sugarcube
trigger_login
end
end
end
def trigger_login
# send the username and password to our controller
trigger :login, get(:username_field).text.to_s, get(:password_field).text.to_s
end
def textFieldShouldReturn(field)
if field == get(:password_field)
trigger_login
else
get(:password_field).becomeFirstResponder
end
end
end
The layout can be tested independently of the controller.
describe LoginLayout do
before do
@subject = LoginLayout.new(root: UIView.new).build
end
it "triggers :login with username/password when the login button is tapped" do
@subject.on :login do |user, password|
user.should == "example"
password.should == "testing123"
end
@subject.get(:username_field).text = "example"
@subject.get(:password_field).text = "testing123"
# Simulate tap on button
@subject.get(:login_button).target.send(@subject.get(:login_button).action)
end
end
The Controller focuses on the movement of the user and the state; the Layout handles displaying the UI state and responding to events.
MotionKit::Events is a very lightweight gem. But using it to decouple your UI from your controller can provide a huge long term benefit in terms of keeping your code maintainable!
The sample app (most of the code is in app/ios/login/) includes a working version of this example.
The example and specs all require SugarCube to run; this is just because I
wanted to have the specs make sure that the on
method (used in so many gems)
behaves the way you would expect it to.
FAQs
Unknown package
We found that motion-kit-events demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.
Security News
CISA’s 2025 draft SBOM guidance adds new fields like hashes, licenses, and tool metadata to make software inventories more actionable.
Security News
A clarification on our recent research investigating 60 malicious Ruby gems.